itachi
itachi

Reputation: 302

Traits specialization

I would like to define a storage type inside trait specializations. But for certain cases I do not have anything to really define i.e. there is no storage type for certain specializations. Of course using void type is not the way, but I'm looking for something similar. I'm not sure if I'm heading the right direction - one way could be to just use a boolean type since it would take up least space. What would be the right/good way to overcome this. I'm not sure if such a problem has been asked. I did not know what to search for!

template<typename T>
struct Traits
{
}     

template<>
struct Traits<TypeA>
{
    typedef std::vector<double> storage;
}     

template<>
struct Traits<TypeB>
{
    typedef std::vector<string> storage;
}

 template<>
struct Traits<TypeC>
{
    //I do not want to specify a storage type here. More like it does not exist. 
    //So what is the correct way to define such a type
    typedef ??void?? storage;
}


int main()
{
    typename Traits<TypeA>::storage myType;
    /*
    do domething
    */
}

Upvotes: 0

Views: 240

Answers (1)

cdhowie
cdhowie

Reputation: 169018

Just omit the storage typedef where it doesn't make sense:

template<>
struct Traits<TypeC>
{
};

Now usage of Traits<TypeC>::storage becomes an error, because it doesn't name a type.

Other notes:

  • You need semicolons after a struct declaration/definition.
  • Your Traits template (not the specialization) should have no body unless there is one that makes sense for every type. That is, it should be template <typename> struct Traits;. This will cause usage of it with a template argument that doesn't make sense to cause errors.

Upvotes: 2

Related Questions