user3770392
user3770392

Reputation: 473

std::conditional with SFINAE

Is it possible to create a sort of std::enable_if_and_else, like std::conditional but without the compile time errors for classes that are not defined.

Here is an example:

static constexpr bool myExpr = true;

struct A {};
struct B;

struct C :
    std::conditional<myExpr,
      A,
      B>::type
    {};  // Compilation error: B is declared but not defined

struct D :
    enable_if_else<myExpr,
      A,
      B>::type
    {};  // It works

Thanks

Upvotes: 4

Views: 1178

Answers (1)

Jonathan Wakely
Jonathan Wakely

Reputation: 171333

Is it possible to create a sort of std::enable_if_and_else, like std::conditional but without the compile time errors for classes that are not defined.

There shouldn't any errors for std::conditional<true, A, B>::type if B is incomplete, because you're not using B in a way that requires it to be complete.

So std::conditional is already what you're looking for.

Upvotes: 6

Related Questions