그렇지 않으면 작동하지 않습니다, 다음은 보호로 작동 할 수 있지만, 멤버 변수에 액세스 할 수 있도록 (public
)가 :
#include <type_traits>
class Test
{
public:
int* p;
};
template< typename T >
typename std::enable_if< std::is_pointer< decltype(T::p) >::value >::type
foo(T bla) { static_assert(sizeof(T) == 0, "T::p is a pointer"); }
template< typename T >
void foo(T bla)
{
}
int main()
{
Test test;
foo(test);
}
물론
Live example
당신이 알 필요가 검사 할 멤버 변수의 이름. C++에 내장 된 일반적인 반사 메커니즘이 없기 때문입니다.
모호성이 has_pointer
도우미 만드는 것입니다 피할 수있는 또 다른 방법 : 단순히 멋진을 얻을 수있는 함수에 첫 번째 라인으로 static_assert
을 추가
template< typename, typename = void >
struct has_pointer : std::false_type {};
template< typename T >
struct has_pointer< T, typename std::enable_if<
std::is_pointer< decltype(T::p) >::value
>::type > : std::true_type {};
template< typename T >
void foo(T bla)
{
static_assert(!has_pointer<T>::value, "T::p is a pointer");
// ...
}
Live example
주 , 읽을 수있는 오류 메시지.
template< typename T >
typename std::enable_if< !has_pointer<T>::value >::type
foo(T bla)
{
// ...
}
foo()에 대한 반환 유형을 선언해야합니다. – alexolut
http://www.gockelhut.com/c++/articles/has_member –
템플릿 기능에서 무엇을하고 싶습니까? 클래스에서 포인터와 어떻게 연관되어 있습니까? – alexolut