0

Given the following scenario:

class A {};
class B : public A {};
class C : public A {};
class D : public A {};

template<typename T/*std::enable_if and std::is_base_of here*/> class X {};

When I declare an X

Then I want to constraint the typename T to obligatorily be a sub-class of A, otherwise I'll get a compiling error.

int main()
{
   X<B> x1 = {}; //should work;
   X<C> x2 = {}; //should work;
   X<D> x2 = {}; //should work;
   X<std::string> = {};  //should generate a compiling error;
   X<int> = {};  //should generate a compiling error;
};

3 Answers 3

4

A correct syntax for using enable_if is

template<typename T, std::enable_if_t<std::is_base_of_v<A, T>, bool> = true> 
class X {};

and now if A is a base of T then std::is_base_of_v<A, T> is true and std::enable_if_t becomes bool and we give it the value of true. If A is not a base of T then the condition is false and std::enable_if_t results to nothing and the template is discarded as a viable candidate and a compiler error will be generated.

3
  • You can also do std::enable_if_t<std::is_base_of_v<A, T>, std::nullptr_t> = nullptr, then you don't have to worry about user messing with the second template parameter. Nov 12, 2020 at 16:57
  • @HolyBlackCat How is that any different? You're using std::nullptr_t and nullptr instead of bool and true, which is more typing. Nov 12, 2020 at 16:58
  • With nullptr_t there's only one possible template argument, so the user can't create more than one specialization. I admit that it's somewhat paranoid though. Nov 12, 2020 at 17:03
3

Alternatively, static_assert might be enough:

template<typename T>
class X {
    static_assert(std::is_base_of<A, T>::value, "!");
};
4
  • I would put a diagnostic string in the static_assert. compilers are crazy.
    – Red.Wave
    Nov 12, 2020 at 17:17
  • @Red.Wave: I don't see meaningful message with doesn't paraphrase the condition. especially in toy example.
    – Jarod42
    Nov 12, 2020 at 17:26
  • @interjay: I didn't notice C++11 flag :/
    – Jarod42
    Nov 12, 2020 at 17:36
  • Come on. Don't be stingy; throw some more words. One major reason for inclusion of static_assert was that compiler generated diagnostics were messy. Otherwise static_assert can be implemented as a crazy macro.
    – Red.Wave
    Nov 13, 2020 at 18:50
3

A third and likewise common variation to

  • static_assert from Jarod42:s answer, and
  • using std::enable_if_t as the type of a non-type template parameter defaulted to some value (template<..., std::enable_if_t<..., bool> = true>) from NathanOliver's answer

is to use std::enable_if_t as the default template argument to a type template parameter (as compared as the type of a non-type template parameter):

template<typename T, 
         typename = std::enable_if_t<std::is_base_of_v<A, T>>> 
class X {};

this is somewhat briefer than using the non-type template parameter approach.

It comes with the drawback, however, that when used for function templates it cannot be combined with, say SFINAE-overloading two variations solely based on opposite results for a predicate:

// Not OK.
template<typename T, 
         typename = std::enable_if_t<std::is_base_of_v<A, T>>> 
void foo(const T&) { /* some impl */ }

// Error: re-definition of foo
template<typename T, 
         typename = std::enable_if_t<!std::is_base_of_v<A, T>>>
void foo(const T&) { /* another impl */ }

as both overloads differ only in their default template arguments, which is not part of the function template's signature, they declare two different function templates with the same signature, which is illegal.

If we non-type template approach instead, this is not an issue:

// OK
template<typename T, 
         std::enable_if_t<std::is_base_of_v<A, T>>* = nullptr> 
void foo(const T&) { /* some impl */ }


template<typename T, 
         std::enable_if_t<!std::is_base_of_v<A, T>>* = nullptr> 
void foo(const T&) { /* another impl */ }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.