jeudi 7 juin 2018

Raising compile-time errors

Is there a way to raise a compile-time error when a certain specialization of a function template is attempted ?

Say

template<typename T> T Factorial(T n) { ... }

short n;
cout << Factorial(n); // Error, short is too small

And is there a way to achieve the same effect for specific methods of a templated class ?

Say

template<typename T> class Arithmetic
{
  T n;
  T GCD(T m) { ... }
  T Factorial() { ... }
};

Arithmetic<short> A;
short m;

cout << A.GCD(m); // Ok
cout << A.Factorial(); // Error, short is too small

Aucun commentaire:

Enregistrer un commentaire