如何指定有效模板参数所需的内容?我的意思是让我们举例如下:
template<class T>
void f(const T& obj)
{
//do something with obj
}
Run Code Online (Sandbox Code Playgroud)
但我希望T只是整数类型所以我会接受char,int,short unsigned等但没有别的.是否(我确定有)一种方法来检测它作为模板arg提供什么?
谢谢.
你可以使用boost :: enable_if和boost :: is_integral(也包含在TR1中):
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_integral.hpp>
template <typename T>
typename boost::enable_if<boost::is_integral<T> >::type
f(const T & obj)
{
...
}
Run Code Online (Sandbox Code Playgroud)