64,637
社区成员
发帖
与我相关
我的任务
分享
template <typename T> struct Checker;
template <> struct Checker<int> { typedef int Type; };
template <> struct Checker<long> { typedef long Type; };
template <typename T>
typename Checker<T>::Type
test1(T var)
{
return var;
}
int main()
{
int i = 1;
long l = 2L;
double d = 3.0;
test1(i);
test1(l);
//test1(d); //该语句会编译失败
return 0;
}
template <typename T>struct integer_func
{
typedef typename integer_traits <T>::int_type int_t;
typedef const int_t cint_t;
int_t Test(int_t var) { return var;};
};
int _tmain(int argc, _TCHAR* argv[])
{
integer_func<int> int_func;
integer_func<long> long_func;
// 下面一个语句不能编译,因为没有针对 char ,对 integer_traits 模板类型 做偏特化
// integer_func<char> char_func;
int i = int_func.Test(8);
long j = long_func.Test(9);
cout << i << endl;
cout << j << endl;
}
template<class T>
typename std::enable_if < std::is_same<int, T>::value || std::is_same<long, T>::value , void >::type
func( T v )
{
};
[/quote]
编译时显示错误,如下:
error: 'enable_if' in namespace 'std' does not name a type
error: expected unqualified-id before '<' token[/quote]
啥版本的啥编译器?[/quote]
codeblocks 里的 GNU GCC 编译器[/quote]
加上
#include <type_traits>[/quote]
增加了一个新错误,如下:
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|[/quote]
gcc 如果可能,能找到多高的版本就用多高的版本。
现在 gcc 4.9 和 5.2 都已经有了(mingw 不清楚)。
另外你需要按照编译提示加上 -std=c++11。[/quote]
也就是说这种方法 只针对高版本的编译器。适用性不广。[/quote]
低版本编译器,可以参考 boost 。
struct false_type
{
const static bool value = false;
};
struct true_type
{
const static bool value = true;
};
template<typename, typename>
struct is_same
: public false_type { };
template<typename _Tp>
struct is_same<_Tp, _Tp>
: public true_type { };
template<bool, typename _Tp = void>
struct enable_if
{ };
// Partial specialization for true.
template<typename _Tp>
struct enable_if<true, _Tp>
{ typedef _Tp type; };
template<class T>
typename enable_if < is_same<int, T>::value || is_same<long, T>::value , void >::type
func( T v )
{
};
template<class T>
typename std::enable_if < std::is_same<int, T>::value || std::is_same<long, T>::value , void >::type
func( T v )
{
};
[/quote]
编译时显示错误,如下:
error: 'enable_if' in namespace 'std' does not name a type
error: expected unqualified-id before '<' token[/quote]
啥版本的啥编译器?[/quote]
codeblocks 里的 GNU GCC 编译器[/quote]
加上
#include <type_traits>[/quote]
增加了一个新错误,如下:
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|[/quote]
gcc 如果可能,能找到多高的版本就用多高的版本。
现在 gcc 4.9 和 5.2 都已经有了(mingw 不清楚)。
另外你需要按照编译提示加上 -std=c++11。[/quote]
也就是说这种方法 只针对高版本的编译器。适用性不广。template<class T>
typename std::enable_if < std::is_same<int, T>::value || std::is_same<long, T>::value , void >::type
func( T v )
{
};
[/quote]
编译时显示错误,如下:
error: 'enable_if' in namespace 'std' does not name a type
error: expected unqualified-id before '<' token[/quote]
啥版本的啥编译器?[/quote]
codeblocks 里的 GNU GCC 编译器[/quote]
加上
#include <type_traits>[/quote]
增加了一个新错误,如下:
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|[/quote]
gcc 如果可能,能找到多高的版本就用多高的版本。
现在 gcc 4.9 和 5.2 都已经有了(mingw 不清楚)。
另外你需要按照编译提示加上 -std=c++11。template<class T>
typename std::enable_if < std::is_same<int, T>::value || std::is_same<long, T>::value , void >::type
func( T v )
{
};
[/quote]
编译时显示错误,如下:
error: 'enable_if' in namespace 'std' does not name a type
error: expected unqualified-id before '<' token[/quote]
啥版本的啥编译器?[/quote]
codeblocks 里的 GNU GCC 编译器[/quote]
加上
#include <type_traits>[/quote]
增加了一个新错误,如下:
d:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\c++0x_warning.h|32|error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.|template<class T>
typename std::enable_if < std::is_same<int, T>::value || std::is_same<long, T>::value , void >::type
func( T v )
{
};
[/quote]
编译时显示错误,如下:
error: 'enable_if' in namespace 'std' does not name a type
error: expected unqualified-id before '<' token[/quote]
啥版本的啥编译器?[/quote]
codeblocks 里的 GNU GCC 编译器[/quote]
加上
#include <type_traits>template<class T>
typename std::enable_if < std::is_same<int, T>::value || std::is_same<long, T>::value , void >::type
func( T v )
{
};
[/quote]
编译时显示错误,如下:
error: 'enable_if' in namespace 'std' does not name a type
error: expected unqualified-id before '<' token[/quote]
啥版本的啥编译器?[/quote]
codeblocks 里的 GNU GCC 编译器