我的代码应该是确定给定函数是否将给定类型作为参数。回答您未来的“做什么”问题,我将很快回答:将其与boost::enable_if
模板一起使用。
该代码使用 C++11 的 decltype 运算符。我的问题是:是否可以使用 c++03 实现相同的目标?
#include <iostream>
template <class F, class P>
struct has_arg_of_type
{
static bool const value = false;
};
template <class R, class A>
struct has_arg_of_type<R (A), A>
{
static bool const value = true;
};
template <class R, class T, class A>
struct has_arg_of_type<R (T::*)(A), A>
{
static bool const value = true;
};
int pisz(int);
class MyClass
{
public:
void pisz(int);
};
int main(int argc, char *argv[])
{
std::cout << "MyClass::pisz has the int as an argument? " << has_arg_of_type<decltype(&MyClass::pisz), int>::value << std::endl; // Line 32
std::cout << "pisz has the int as an argument? ? " << has_arg_of_type<decltype(pisz), int>::value << std::endl;
std::cout << "pisz has the float as an argument? ? " << has_arg_of_type<decltype(pisz), float>::value << std::endl;
return 0;
}
错误是:
In function 'int main(int, char**)':
Line 32: error: 'MyClass::pisz(int)' cannot appear in a constant-expression