클래스의 템플릿 매개 변수의 멤버 함수 결과를 가져와야합니다. 불행히도, 나는 C++ 03에 바인딩되어 있고 decltype을 사용할 수 없지만 tr1 :: result_of를 사용할 수 있습니다. 나는 다음과 같은 코드를 시도하지만 내 컴파일러와 함께 작동하지 않았다 (GCC 4.3, 나는이를 변경할 수 없습니다) : 나는 통과템플릿 매개 변수의 멤버 함수 호출 결과
If Fn is a pointer to a non-static member function, and the first type in ArgTypes is the class the member belongs to (or a reference to it, or a reference to a derived type, or a pointer to it), and the remaining types in ArgTypes describe its arguments.
:
#include <tr1/functional>
struct ReturnType {};
struct O
{
ReturnType f();
};
template<typename T> struct A
{
typename std::tr1::result_of<(&T::f)(T*)>::type f();
};
void f()
{
A<O> a;
ReturnType x = a.f();
}
위의 코드는 result_of<Fn(ArgTypes ...)
에 대한 이해를 반영 그것은 멤버 함수에 대한 포인터이며 첫 번째 매개 변수 유형을 클래스에 대한 포인터로 지정합니다. 그러나 컴파일러는 다음 오류를 인쇄합니다.
result_of.cpp:12: error: `&' cannot appear in a constant-expression
result_of.cpp:12: error: a function call cannot appear in a constant-expression
result_of.cpp:12: error: template argument 1 is invalid
result_of.cpp:12: error: invalid use of ‘::’
result_of.cpp:12: error: expected ‘;’ before ‘f’
result_of.cpp: In function ‘void f()’:
result_of.cpp:18: error: ‘struct A<O>’ has no member named ‘f’
결과 typedef를 추가하십시오, 그래서 나는 컴파일 타임에 반환 유형을 얻을 수 있어야합니다.
decltype없이 멤버 함수의 결과 형식을 가져 오는 방법이 있습니까? 필자가 인용 한 문서에서 멤버 함수 포인터에 대한 전문화가있을 것으로 기대하지만 잘못된 것 같습니다. – Jens
멤버 함수 포인터 * type *은 전달할 수 있지만 멤버 함수 포인터는 전달할 수 없습니다. 이는 근본적인 제한 사항입니다. 'mytemplate '과 같이 사용할 수있는 클래스 템플릿은'X'와'Y'로 쓸 수 없습니다. 클래스 템플릿은 유형이 아닌 매개 변수의 유형을 알아야합니다. –