Search code examples
c++templatesstaticdatamember

Standard says, we can define static data member template like class template and function template, but how exactly?


The chapter of Templates in the C++03 Standard starts with the following:

A template defines a family of classes or functions.

template-declaration:  
     exportopt template < template-parameter-list > declaration
template-parameter-list:
     template-parameter
     template-parameter-list , template-parameter

The declaration in a template-declaration shall

— declare or define a function or a class, or

— define a member function, a member class or a static data member of a class template or of a class nested within a class template, or

— define a member template of a class or class template.

A template-declaration is a declaration. A template-declaration is also a definition if its declaration defines a function, a class, or a static data member.

So from what I understand reading the bold text is that we can define static data-member-template , in addition to class -template and function-template. But I've never seen static data-member-template. How exactly is it defined? I tried the following, but GCC is not accepting it (ideone):

template<typename T> struct X{};

template<typename T>
struct A
{
   template<typename U> static X<U> data_member;
};

So I started doubting myself if I understood the quotation correctly. What exactly am I missing? Or how exactly can we define static data member template? What does the bold text mean?


Solution

  • No, it refers to:

    template<typename T> int A<T>::staticDataMember;
    //                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~ declaration
    

    If A is a class template like the following

    template<typename T>
    struct A { static int staticDataMember; };