Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++20 Concepts: Difference in the behavior of the compound requirement expression with a pointer-type member in GCC and Clang

Consider the following code (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 successfully compiles this program. But GCC 10.2 fails:

<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

The most interesting part is:

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

As I understand it, in the compound requirement { a.str } expression has type const char*& instead of const char*. So, why is this happening? Which compiler is correct?

like image 679
Stepan Repin Avatar asked Sep 05 '20 20:09

Stepan Repin


People also ask

What is a compound requirement?

You can supply any expression which evaluates to true or false. With a compound requirement we can check the return type of an expression and optional if the expressions result is noexcept. As the name indicates, a compound requirement has the expression in curly braces, followed by the optional noexcept and something like a trailing return-type.

How C++20 concepts can simplify your code?

In my last month post How C++20 Concepts can simplify your code I introduced the different kind of a requires-clause, part of C++20s Concepts. Concepts and the requires-clause allow us to put constraints on functions or classes and other template constructs.

What is a type requirement in C++?

A type requirement is the keyword typename followed by a type name, optionally qualified. The requirement is that the named type is valid: this can be used to verify that a certain named nested type exists, or that a class template specialization names a type, or that an alias template specialization names a type.

What is a requires expression in C++?

But hey, you are reading a post about C++; you had it coming. A requires-expression has a body which itself has one of multiple requirements. The expression can have an optional parameter list. Those a requires-expression looks like a function called requires except for the return-type which is implicitly bool.


1 Answers

By my reading of [expr.prim.req.compound]/1, GCC is correct to emit an error:

The immediately-declared constraint ([temp.param]) of the type-constraint for decltype((E)) shall be satisfied.

With an accompanying example:

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

is equivalent to

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

decltype((a.str)) is indeed const char*&, so I would expect that this is what's passed to same_as.

like image 87
chris Avatar answered Oct 19 '22 00:10

chris