7

The following code is a class template. Outside of the class, I define the static members. However, there are two methods I use for defining the PrimeBlock* static member, method - 1 is using template alias, and it reports redeclaration error; method - 2 could work correctly. I wonder why method - 1 cannot work.

template <typename tree_type>
class Tree {
    struct PrimeBlock {
        vector<tree_type*> vec;
    };

    static tree_type* ROOT;
    static PrimeBlock* primeBlock;
};

template <typename tree_type>
tree_type* ROOT = nullptr;

// method - 1 template alias for definition - error
template <typename tree_type>
using PrimeBlock = typename Tree<tree_type>::PrimeBlock;
template <typename tree_type>
PrimeBlock<tree_type>* Tree<tree_type>::primeBlock = nullptr; // this cannot work due to redeclaration error

// method - 2 - it works
//template <typename tree_type>
//typename Tree<tree_type>::PrimeBlock* Tree<tree_type>::primeBlock = nullptr; // however, this could work, why?

The error message produced by method - 1

error: conflicting declaration ‘PrimeBlock<RtreeVariant>* Tree<RtreeVariant>::primeBlock’
 PrimeBlock<RtreeVariant>* Tree<RtreeVariant>::primeBlock = nullptr;

note: previous declaration as ‘Tree<RtreeVariant>::PrimeBlock* Tree<RtreeVariant>::primeBlock’
 static PrimeBlock* primeBlock;

error: declaration of ‘Tree<RtreeVariant>::PrimeBlock* Tree<RtreeVariant>::primeBlock’ outside of class is not definition [-fpermissive]
 PrimeBlock<RtreeVariant>* Tree<RtreeVariant>::primeBlock = nullptr;
Elaine
  • 111
  • 6
  • No repro with clang, I do get the error with gcc though. – Jorn Vernee Jun 09 '17 at 18:25
  • @JornVernee I use g++ 6.3.1 and plan to stick on g++. Do you know how to fix the method - 1? – Elaine Jun 09 '17 at 18:48
  • From my perspective `PrimeBlock` might be a templated entity (cf. [here](http://en.cppreference.com/w/cpp/language/templates)) rather than a class template, in which case you cannot instantiate it like a plain class template... – W.F. Jun 09 '17 at 18:51

0 Answers0