static member specialization of templated child class and templated base class

Posted by b3nj1 on Stack Overflow See other posts from Stack Overflow or by b3nj1
Published on 2012-07-11T09:02:46Z Indexed on 2012/07/11 9:15 UTC
Read the original article Hit count: 186

Filed under:
|
|
|

I'm trying to have a templated class (here C) that inherits from another templated class (here A) and perform static member specialization (of int var here), but I cant get the right syntax to do so (if it's possible

#include <iostream>

template<typename derived>
class A
{
    public:
        static int var;
};

//This one works fine
class B
    :public A<B>
{
    public:
        B()
        {
            std::cout << var << std::endl;
        }
};
template<>
int A<B>::var = 9;

//This one doesn't works
template<typename type>
class C
    :public A<C<type> >
{
    public:
        C()
        {
            std::cout << var << std::endl;
        }
};
//template<>
template<typename type>
int A<C<type> >::a = 10;

int main()
{
    B b;
    C<int> c;
    return 0;
}

I put an example that works with a non templated class (here B) and i can get the static member specialization of var, but for C that just doesn't work.

Here is what gcc tells me :

test.cpp: In constructor ‘C<type>::C()’:
test.cpp:29:26: error: ‘var’ was not declared in this scope
test.cpp: At global scope:
test.cpp:34:18: error: template definition of non-template ‘int A<C<type> >::a’

I'm using gcc version 4.6.3, thanks for any help

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates