【问题标题】:std::conditional with SFINAE带有 SFINAE 的 std::conditional
【发布时间】:2017-04-06 09:17:39
【问题描述】:

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

谢谢

【问题讨论】:

  • 不应该 myExprtrue 所以它需要 A 分支?
  • 是的,错误的复制和粘贴。你说得对。谢谢
  • 无论如何,I cannot reproduce the error。你用的是什么编译器?
  • 你说得对,我使用的代码不同。我试图做一个基本案例,但它似乎不同..我会再检查一次

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


【解决方案1】:

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

如果B 不完整,std::conditional&lt;true, A, B&gt;::type 不应该有任何错误,因为您没有以需要完整的方式使用B

所以std::conditional 已经是你要找的了。

【讨论】:

  • 是的,你是对的。我使用的代码不同,我只是尝试将其简化为基本情况,但存在一些差异。我将编辑问题。感谢指出这一点
  • 好的,我发现我的代码有什么问题。我已经以多种方式测试了 std::conditional ,您是完全正确的。再次感谢您
猜你喜欢
  • 2021-10-24
  • 2021-07-24
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 2021-04-24
相关资源
最近更新 更多