Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::conditional with SFINAE [duplicate]

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

like image 981
user3770392 Avatar asked Oct 23 '25 17:10

user3770392


1 Answers

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.

like image 66
Jonathan Wakely Avatar answered Oct 25 '25 06:10

Jonathan Wakely