【问题标题】:Why can't I get compilation error of custom concept supplied with initializer list?为什么我不能得到初始化列表提供的自定义概念的编译错误?
【发布时间】:2021-05-22 07:38:33
【问题描述】:

假设我有 3 个概念:

  • ostreamable
  • istreamable
  • iostreamable

定义在哪里:

template <typename T>
concept ostreamable = requires (std::ostream& os, T arg) {
    {os << arg} -> std::convertible_to<std::ostream&>;
};
template <typename T>
concept istreamable = requires (std::istream& is, T& arg) {
    {is >> arg} -> std::convertible_to<std::istream&>;
};
template <typename T>
concept iostreamable = ostreamable<T> && istreamable<T>;

应用:

iostreamable auto var1 = {4, 5, 10, 10}; // no error, but unexpected.
iostreamable auto var2 = 3.232; // no error, expected
iostreamable auto var3 = std::bitset<4>{0b1001}; // no error, expected
iostreamable auto var4 = std::vector{4, 10, 3, 10}; // compilation error, expected

当我将var1 与上述概念iostreamable 的要求一起使用时,例如使用operator&lt;&lt;operator&gt;&gt;,我预计会得到详细的编译错误。

【问题讨论】:

    标签: c++ initializer-list c++-concepts


    【解决方案1】:

    这似乎是一个 GCC 错误。

    Clang 和 MSVC 拒绝您的代码。

    【讨论】:

      猜你喜欢
      • 2012-02-13
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      相关资源
      最近更新 更多