Ilia
Ilia

Reputation: 3

Using typedef inside template specializations

I'am newbie in c++, and i'm trying to write an additional template which contains type char or type int. After that I want to use this template in other template to select the type of data, depending of input data padding.

template <bool isPadding>
class PaddingTemplate;

template <>
class PaddingTemplate<false>
{
public:
    typedef char Type;
};

template <>
class PaddingTemplate<true>
{
public:
   typedef int Type;
};

template <class T, bool Padding = ((sizeof(T) % sizeof(int)) == 0)>
class ObjectComparator
{
private:
    typedef     PaddingTemplate<Padding>    PaddingTrick;
    typename    PaddingTrick::Type          DataType;

    DataType Shadow[sizeof(T) / sizeof(DataType)];
};

I get this compiler error

Compiler msg

How to solve the problem and automate the choice of data type depending on data padding?

Upvotes: 0

Views: 185

Answers (1)

Alex Huszagh
Alex Huszagh

Reputation: 14614

DataType isn't actually a type. It's a instance of PaddingTrick::Type, which is why the next line is giving you an error. You need to do:

typedef typename PaddingTrick::Type DataType;

Upvotes: 4

Related Questions