std::conditional 与 SFINAE

std::conditional with SFINAE

是否可以创建一种 std::enable_if_and_else,如 std::conditional 但没有未定义的 类 的编译时错误。

这是一个例子:

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

谢谢

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.

如果 B 不完整,std::conditional<true, A, B>::type 应该不会有任何错误,因为您没有以要求它完整的方式使用 B

所以 std::conditional 已经是您要找的东西了。