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;