【问题标题】:C++20 Concepts: Difference in the behavior of the compound requirement expression with a pointer-type member in GCC and ClangC++20 概念:在 GCC 和 Clang 中具有指针类型成员的复合需求表达式的行为差异
【发布时间】:2020-12-24 17:41:12
【问题描述】:

考虑以下代码 (Godbolt):

#include <iostream>
//#include <concepts>
#include <type_traits>

// Since the latest clang doesn't have <concepts>,
// took this here: https://en.cppreference.com/w/cpp/concepts/same_as
// Using of std::same_as still gives an error in GCC.
namespace detail {
    template< class T, class U >
    concept SameHelper = std::is_same_v<T, U>;
}

template< class T, class U >
concept same_as = detail::SameHelper<T, U> && detail::SameHelper<U, T>;

template<typename T>
concept HasStr = requires(T a) { { a.str } -> same_as<const char*>; };

struct A {
    const char* str = "A";
};

const char* f(HasStr auto has_str) {
    return has_str.str;
}

int main() {
    A a;
    std::cout << f(a) << "\n";
    return 0;
}

Clang 10.0.1 成功编译了这个程序。但是 GCC 10.2 失败了:

<source>: In function 'int main()':
<source>:28:21: error: use of function 'const char* f(auto:11) [with auto:11 = A]' with unsatisfied constraints
   28 |     std::cout << f(a) << "\n";
      |                     ^
<source>:22:13: note: declared here
   22 | const char* f(HasStr auto has_str) {
      |             ^
<source>:22:13: note: constraints not satisfied
<source>: In instantiation of 'const char* f(auto:11) [with auto:11 = A]':
<source>:28:21:   required from here
<source>:16:9:   required for the satisfaction of 'HasStr<auto:11>' [with auto:11 = A]
<source>:16:18:   in requirements with 'T a' [with T = A]
<source>:16:38: note: 'a.str' does not satisfy return-type-requirement, because
   16 | concept HasStr = requires(T a) { { a.str } -> same_as<const char*>; };
      |                                    ~~^~~
<source>:16:36: error: deduced expression type does not satisfy placeholder constraints
   16 | concept HasStr = requires(T a) { { a.str } -> same_as<const char*>; };
      |                                  ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:16:36: note: constraints not satisfied
<source>:9:13:   required for the satisfaction of 'SameHelper<T, U>' [with T = const char*&; U = const char*]
<source>:13:9:   required for the satisfaction of 'same_as<const char*&, const char*>'
<source>:9:31: note: the expression 'is_same_v<T, U> [with T = const char*&; U = const char*]' evaluated to 'false'
    9 |     concept SameHelper = std::is_same_v<T, U>;
      |                          ~~~~~^~~~~~~~~~~~~~~
Compiler returned: 1

最有趣的部分是:

<source>:9:31: note: the expression 'is_same_v<T, U> [with T = const char*&; U = const char*]' evaluated to 'false'

据我了解,在复合要求中 { a.str } 表达式的类型为 const char*&amp; 而不是 const char*。那么,为什么会这样呢?哪个编译器是正确的?

【问题讨论】:

    标签: c++ c++20 c++-concepts


    【解决方案1】:

    根据我对[expr.prim.req.compound]/1 的阅读,GCC 发出错误是正确的:

    应满足 decltype((E)) 的类型约束的立即声明的约束 ([temp.param])。

    附上一个例子:

    requires {
      { E1 } -> C;
      { E2 } -> D<A₁, ⋯, An>;
    };
    

    等价于

    requires {
      E1; requires C<decltype((E1))>;
      E2; requires D<decltype((E2)), A₁, ⋯, An>;
    };
    

    decltype((a.str)) 确实是const char*&amp;,所以我希望这是传递给same_as 的内容。

    【讨论】:

    猜你喜欢
    • 2020-12-27
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多