tags:

views:

89

answers:

3
#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

+1  A: 

Maybe you mean:

bool isPoly = IsPolymorphic<T>::value
Alex Farber
+2  A: 

Try this:

template<class T, bool isPoly = IsPolymorphic<T>::value>
Chubsdad
Thanks, but I believe, litb's answer is the one you should be selecting for the benefit of all future references
Chubsdad
@Chubsdad thanks. I think you were one of the first who answered so whatever @A-ha does, I think you deserve the accept-mark too. Cheers :)
Johannes Schaub - litb
+3  A: 

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.
Johannes Schaub - litb
+1: VS always fails me on this one (redeclarting 'T')
Chubsdad
@litb Thanks for your answer I upvoted it. I really value your answer.
There is nothing we can do