Another problem with templates

#include "stdafx.h"
#include <iostream>
using std::cout;

template<class T>
class IsPolymorphic
{
 template<class T>
 struct Check
 {
  enum {value = false};
 };

 template<class T>
 struct Check<T*>
 {
  enum {value = true};
 };
public: 
 enum {value = Check<T>::value};

};

template<bool flag, class T, class U>
struct Select
{
 typedef T value_type;
};

template<class T, class U>
struct Select<true,T,U>
{
 typedef U value_type;
};

template<class T, bool isPoly = IsPolymorphic<T>>
class Container
{
public:
 typedef typename Select<isPoly,T,T*>::value_type value_type;
 Container(){}
};

int _tmain(int argc, _TCHAR* argv[])
{
 //cout << IsPolymorphic<int*>::value;
 Container<int> c;
 return 0;
}

I'm getting following errors:
Error 3 error C2512: 'Container' : no appropriate default constructor available
Error 2 error C2133: 'c' : unknown size
Error 1 error C2975: 'Container' : invalid template argument for 'isPoly', expected compile-time constant expression

As for these errors:
no 3 - clearly there is dflt ctor - so what's going on?
no 2 - why is it unknown size? I've specified int as a type so why would it be unknown?
no 1 - exactly as no 2
Thanks for any help with this.
Thanks to all of you for helping me in solving this problem

Upvotes: 1

Views: 237

Answers (3)

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 506975

Your code has several errors:

  • You try to hide the template parameter T by an inner declaration of that name
  • You use IsPolymorphic<T> intead of IsPolymorphic<T>::value
  • What @potatoswatter says.

Upvotes: 3

Chubsdad
Chubsdad

Reputation: 25497

Try this:

template<class T, bool isPoly = IsPolymorphic<T>::value>

Upvotes: 2

Alex F
Alex F

Reputation: 43311

Maybe you mean:

bool isPoly = IsPolymorphic<T>::value

Upvotes: 1

Related Questions