c++ - std::条件与 SFINAE

标签 c++ c++11 template-meta-programming sfinae enable-if

是否可以创建一种 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.

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

所以 std::conditional已经是您正在寻找的东西。

关于c++ - std::条件与 SFINAE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43250849/

相关文章:

c++ - 原始套接字未接收 ARP 请求

c++ - 聚合初始化不支持构造函数访问

c++ - 在编译时判断虚方法是否被重写

c++ - boost 元组部分迭代?

c++ - 如何启动完全用 C++ 编写的操作系统?

C++空字符数组的长度> 0

c++ - 使用 mpl::if_、boost::function 和 typedef 无效的问题

c++ - 将 std::array 转换为 std::vector

c++ - 使用 clang 编译失败,出现 libstdc++4.4.7 和 -std=c++0x

c++ - 获取对模板参数给定类型对象的虚拟引用