정수 및/또는 이중에 대한 특정 기능이 필요한 입력 검사기를 구축 중입니다 (예 : 'isPrime'은 정수에서만 사용할 수 있어야 함).std :: enable_if : 매개 변수 대 템플릿 매개 변수
나는 그것이 완벽하게 작동하고 매개 변수로 enable_if
을 사용하고있는 경우 : 내가 템플릿 paramater로 사용하고있는 경우 (http://en.cppreference.com/w/cpp/types/enable_if에서 입증 된 바와 같이)
template <class T>
class check
{
public:
template< class U = T, class = typename std::enable_if<std::is_same<U, int>::value>::type >
inline static U readVal()
{
return BuffCheck.getInt();
}
template< class U = T, class = typename std::enable_if<std::is_same<U, double>::value>::type >
inline static U readVal()
{
return BuffCheck.getDouble();
}
};
다음
template <class T>
class check
{
public:
template< class U = T>
inline static U readVal(typename std::enable_if<std::is_same<U, int>::value >::type* = 0)
{
return BuffCheck.getInt();
}
template< class U = T>
inline static U readVal(typename std::enable_if<std::is_same<U, double>::value >::type* = 0)
{
return BuffCheck.getDouble();
}
};
을하지만 다음 오류가 있습니다 :
error: ‘template<class T> template<class U, class> static U check::readVal()’ cannot be overloaded
error: with ‘template<class T> template<class U, class> static U check::readVal()’
두 번째 버전에서는 무엇이 잘못 되었습니까.
아마도 무관하지만 VS2010에서 나는 기본 템플릿 인수가 단지 클래스 템플릿에 허용되기 때문에 그렇게 할 수 없다 -이 현학적이지만'inline' 키워드에 나는 g ++ – David
모르는 멤버 메소드 또는 템플릿이 필요하지 않으며 확실히 템플릿 인 멤버가 아닙니다 ;-) – AJG85