2010-09-16 71 views
1
#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; 
} 

我收到以下错误另一个问题:
错误3错误C2512: '集装箱':没有适当的默认构造函数可用
错误2错误C2133: 'C':未知大小
错误1个错误C2975:“容器”:关于“isPoly”无效模板参数,预期编译时间常量表达式使用模板

至于这些错误:
没有3 - 显然有dflt ctor - 那是怎么回事?
没有2 - 为什么它是未知的大小?我已经指定int为一个类型,为什么它是未知的?
没有1 - 完全是2
感谢您的任何帮助。
感谢大家对我的帮助解决这个问题

+2

在那里搜索“>>”。空白是你的朋友。 – Potatoswatter 2010-09-16 11:26:07

+0

你真的想重新实现标准库的功能吗?特别是,型特质? – Potatoswatter 2010-09-16 11:26:55

+0

@Patatoswatter;)只是想每天学习新的东西。 ;) – 2010-09-16 11:30:36

回答

2

试试这个:

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

谢谢,但我相信,litb的答案是你应该选择的所有未来引用 – Chubsdad 2010-09-16 11:34:05

+0

谢谢。我认为你是第一个回答这个问题的人之一,我认为你也应该接受这个接受标记。欢呼声:) – 2010-09-16 11:41:10

1

也许你的意思是:

 
bool isPoly = IsPolymorphic<T>::value 
3

你的代码中有几个错误:

  • 您尝试隐藏模板pa该名称
  • 的内部声明rameter T您使用IsPolymorphic<T>这一翻译的IsPolymorphic<T>::value
  • 什么@potatoswatter说。
+0

+1:VS总是让我失望(对这个重新声明'T') – Chubsdad 2010-09-16 11:32:47

+0

@litb感谢您的回复,我赞成。我真的很看重你的答案。 – 2010-09-16 17:41:25