Search code examples
c++c++11template-meta-programmingsfinaeenable-if

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


Solution

  • 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.