내 코드는 주어진 함수가 주어진 형식을 매개 변수로 사용하는지 여부를 결정하는 것으로 가정합니다. 당신의 미래에 대한 답을 "무엇을 위해"나는 짧게 대답 할 것입니다 : 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
예, 오류가있었습니다. 나는 codepad (원래 코드를 작성하는데 사용 된 코드 패드)가 C++ 11을 아직 지원하지 않는다고 생각한다. 그러나 코드는 MinGW-32 Qt 5.0 (IIRC g ++ 4.6)에서 컴파일됩니다. – user2146414