【问题标题】:Equivalent static_asserts giving conflicting results for is_array<>等效的 static_asserts 为 is_array<> 提供了冲突的结果
【发布时间】:2015-01-20 13:24:29
【问题描述】:

在下面的 sn-p 中,一个静态断言通过,另一个失败:

template <class Rng> constexpr bool is_array(Rng&& r) {
  // int*** debug = r;  // uncomment this to debug r's type
  return std::is_array<Rng>{};
  // return std::is_array<decltype(r)>{};  // fails too
}

int a[5] = {0, 1, 2, 3, 4};
static_assert(std::is_array<decltype(a)>{}, ""); // passes
static_assert(is_array(a), ""); // fails

提示:去掉注释即可调试类型(正确推断为int [5])。

这是为什么?在 clang 主干上测试。

我猜这与数组衰减为指针有关......不知何故。

解决方案:使用std::remove_reference_tRng 将是int (&amp;)[5],这是对数组的引用,而不是数组。

Xeo 添加:

template<class> struct dump;
dump<decltype(r)>{};

将无法编译并显示r 的正确类型。

int**** j = r; 产生了一个错误错误(说不能将int[5] 分配给int****)。

【问题讨论】:

  • 尝试插入remove_reference
  • @KerrekSB 说得通而且有效,废话。 Clang 错误消息让我失望了,它说无法将 int [5] 分配给 int*** 而不是 int (&amp;)[5] 或类似的。
  • Scott Meyers 查看T 类型的技巧是template&lt;class&gt; class TD /* undefined */; TD&lt;T&gt; foo;
  • 现在应该结束这个问题吗?

标签: c++ c++14 perfect-forwarding universal-reference forwarding-reference


【解决方案1】:

Rng 的类型是int (&amp;)[5],它是对数组(而不是数组)的引用,因此std::is_array 返回false_type

可以删除引用(例如使用std::remove_reference_t)以使其按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    • 2018-08-14
    • 2018-10-17
    • 1970-01-01
    相关资源
    最近更新 更多