가능한 중복 :
C++ Restrict Template Function
Is it possible to write a C++ template to check for a function's existence?템플릿을 제한 할 수 있습니까?
템플릿이 을 인스턴스화 할 수있는 유형을 (즉, 제한 할 수 있나요 내가 template<type_not_allowed>
을 사용하는 경우 컴파일러 오류가)?
가능한 중복 :
C++ Restrict Template Function
Is it possible to write a C++ template to check for a function's existence?템플릿을 제한 할 수 있습니까?
템플릿이 을 인스턴스화 할 수있는 유형을 (즉, 제한 할 수 있나요 내가 template<type_not_allowed>
을 사용하는 경우 컴파일러 오류가)?
한 가지 방법은 기본 구현을 제공하지 않고 허용하려는 유형에만 클래스 템플릿을 전문화하는 것입니다. 예를 들어 :
#include <iostream>
using namespace std;
template<class X> class Gizmo
{
public:
Gizmo();
};
template<> Gizmo<int>::Gizmo()
{
}
int main()
{
Gizmo<float> gf; // ERROR: No specialization for Gizmo<float> results in a linking error
}
는 현재 Restrict Template Function
는 대답 그래서 내가 덧글을 남길 수 없습니다 ...
확인할 수 있습니다 불법 유형에 생성자 개인하십시오 :
template<typename T>
class Z
{
public:
Z() {}
};
template<>
class Z<int>
{
private:
Z();
};
Z<float> f; // OK
Z<int> i; // Compile time error.
좋은 대답, 일을 분명히하기 위해, 예 : 'struct No {}; struct Yes : public No {}; 템플릿 구조체 myfunction_allower {enum {value = true}; }; template <> struct myfunction_allower {enum {value = false}; }; 템플릿 void myfunction (T) {static_assert (myfunction_allower :: 값, "허용되지 않는 유형"); } void test() {myfunction (1.); struct 아니요 아니요; myfunction (no); // 오류 C2338 : 형식이 허용되지 않습니다 struct 예 예; myfunction (예); // 작동합니다 : 금지는 전이가 아닙니다.' –
reder