Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ syntax to call templated inner class static member function?

I have some template code which compiles fine in VC9 (Microsoft Visual C++ 2008) but won't compile in GCC 4.2 (on Mac). I'm wondering if there's some syntactical magic that I'm missing.

Below I have a stripped-down example which demonstrates my error. Sorry if this example seems meaningless, I removed as much as I could to isolate this error.

In particular I have a template class S which has an inner class R which is also a template class. From a top-level template function foo, I am trying to call R::append which is a static member function of R:

template< typename C >
struct S {
    template< typename T >
    S<C> & append( const T & ) { return *this; }

    template< int B >
    struct R {
        template< typename N >
        static S<C> & append( S<C> & s, const N ) {
            return s.append( 42 );
        }
    };
};

template< typename C >
S<C> & foo( S<C> & s, const int n ) {
    S<C>::R<16>::append( s, n ); // error: '::append' has not been declared
    return s;
}

Anyone out there know what I'm doing wrong?

like image 218
jwfearn Avatar asked Dec 29 '22 05:12

jwfearn


2 Answers

I get it to compile with:

template< typename C >
S<C> & foo( S<C> & s, const int n ) {
    typedef typename S<C>::template R<16> SR;
    SR::append( s, n );
    return s;
}
like image 140
Magnus Hoff Avatar answered Jan 27 '23 08:01

Magnus Hoff


You have to tell the compiler that the dependent name R is a template:

template< typename C >
S<C> & foo( S<C> & s, const int n ) {
    S<C>::template R<16>::append( s, n );
    return s;
}
like image 37
sth Avatar answered Jan 27 '23 08:01

sth