好奇地反复出现模板模式问题 [英] curiously recurring template pattern problem
问题描述
以下代码无法编译
template< class Derived>
struct X
{
typedef typename Derived :: type type;
};
struct Y:public X< Y>
{
typedef int type;
};
int main()
{
}
gcc和VC ++ 7给出了无用的错误消息,但是Comeau C ++说''不完整
类型是不允许的''指着X中的typedef。
有人可以向我解释为什么这应该是一个问题吗?我假设
这种结构很难或不可能编译为某些
的原因。
其次是有一种方法可以实现我想要的,这就是让X知道从X派生的任何类中定义的类型的定义,方法是将
派生类型作为X的模板参数(又名奇怪的重复
模板模式)。
谢谢,
john
The following code does not compile
template <class Derived>
struct X
{
typedef typename Derived::type type;
};
struct Y : public X<Y>
{
typedef int type;
};
int main()
{
}
gcc and VC++ 7 give unhelpful error messages but Comeau C++ says ''incomplete
type is not allowed'' pointing at the typedef in X.
Can someone explain to my why this should be a problem? I''m presuming that
this kind of construct is difficult or impossible to compile for some
reason.
Secondly is there a way to achieve what I want, which is to let X know the
definition of a type defined in any class derived from X, by passing the
derived type as a template parameter to X (aka the curiously recurring
template pattern).
Thanks,
john
推荐答案
John Harrison写道:
John Harrison wrote:
以下代码无法编译
模板< class派生>
struct X
{
typedef typename Derived :: type type;
};
struct Y:public X< Y>
此时,Y不完整,但必须根据Y实例化X.
编译器还不知道Y :: type。至少那是我认为它的方式
有效。我不知道你问题的解决方案。
{
typedef int type;
};
int main()
{
}
The following code does not compile
template <class Derived>
struct X
{
typedef typename Derived::type type;
};
struct Y : public X<Y>
At this point, Y is incomplete, but X must be instantiated based on Y.
The compiler doesn''t yet know Y::type. At least that''s how I think it
works. I don''t know a solution to your problem.
{
typedef int type;
};
int main()
{
}
John Harrison写道:
John Harrison wrote:
以下代码执行不编译
模板< class Derived>
struct X
{
typedef typename Derived :: type type;
};
<结构Y:公共X< Y>
此时,Y不完整,但必须根据Y实例化X.
编译器还不知道Y :: type。至少那是我认为它的方式
有效。我不知道你问题的解决方案。
{
typedef int type;
};
int main()
{
}
The following code does not compile
template <class Derived>
struct X
{
typedef typename Derived::type type;
};
struct Y : public X<Y>
At this point, Y is incomplete, but X must be instantiated based on Y.
The compiler doesn''t yet know Y::type. At least that''s how I think it
works. I don''t know a solution to your problem.
{
typedef int type;
};
int main()
{
}
John Harrison写道:
John Harrison wrote:
[snip] <第二,有一种方法可以实现我想要的,即通过将
派生类型作为模板参数传递,让X知道从X派生的任何类中定义的类型的定义。到X(又名奇怪的反复模板模式)。
谢谢,
约翰
[snip]
Secondly is there a way to achieve what I want, which is to let X know the
definition of a type defined in any class derived from X, by passing the
derived type as a template parameter to X (aka the curiously recurring
template pattern).
Thanks,
john
怎么样:
模板< class DerivedTraits,类Derived>
struct X
{
// typedef typename派生: :类型类型;
typedef typename DerivedTraits :: type type;
};
struct YTraits {
typedef int type;
};
struct Y:public YTraits,X< YTraits,Y>
{
// typedef int type;
};
Denis
How about this:
template <class DerivedTraits, class Derived>
struct X
{
// typedef typename Derived::type type;
typedef typename DerivedTraits::type type;
};
struct YTraits {
typedef int type;
};
struct Y : public YTraits, X<YTraits, Y>
{
// typedef int type;
};
Denis
这篇关于好奇地反复出现模板模式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!