我的代碼應該是確定給定函數是否將給定類型作爲參數。回答你的未來「我爲什麼」的問題,我將很快回答:與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
是的,我知道有一個錯誤。我認爲這個鍵盤(最初是我用來編碼的)不支持C++ 11。但代碼編譯在MinGW-32 Qt 5.0(IIRC g ++ 4.6) – user2146414 2013-03-26 19:41:13