开发者

No class template named X in templated class

When tryin to compile this (CRTP-like) code with GCC 4.6.0:

template<template&开发者_高级运维lt;class> class T> struct A;

template<class T> 
struct B: A<B<T>::template X> {
    template <class U> struct X { U mem; };
};

B<int> a;

I get the errormessage "test.cpp:3:26: error: no class template named ‘X’ in ‘struct B<int>’". Why does X seem to be invisible outside the class definition?


As Emile Cormier correctly points out here the problem is that at the place of instantiation of A, B is still an incomplete type, and you cannot use the inner template.

The solution for that is moving the template X outside of the template B. If it is independent of the particular instantiation T of the template B, just move it to the namespace level, if it is dependent on the instantiation, you can use type traits:

template <typename T>
struct inner_template 
{
   template <typename U> class tmpl { U mem; }; // can specialize for particular T's
};
template <typename T>
struct B : A< inner_template<T>::template tmpl >
{
};


struct B is still considered an incomplete type when you specify A<B<T>::template X> as the base class.


You're trying to use a member of B as a parent of B creating a recursive-esque situation. For example this doesn't compile either:

template<template<class> class T> struct A {};

struct B : public A<B::nested>
{
        struct nested {};
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜