Refers to the requires clause introduced in c++ 20
Questions tagged [requires-clause]
10 questions
6
votes
2 answers
Why can't a member function require a static constexpr member of the same class to be true?
I try to have a member function require a static constexpr boolean member to be true. This would be very helpful to DRY a quite complex requirement. And I do not quire get the reason why the compiler won't let me.
Minimal example with slightly less…

tirofes
- 63
- 2
3
votes
1 answer
accessing member in trailing return type vs. in requires clause of a locally defined friend function
Example code as below or on godbolt. All friend functions compile with gcc and Visual Studio. clang fails when trying to access S::foo() in trailing return type.
If clang is correct, why is member access into incomplete type not allowed in…

wanghan02
- 1,227
- 7
- 14
3
votes
1 answer
Is the expression `requires { typename enable_if_t>; }` the same as `fun_v` "by definition" for any meta-predicate fun_v?
I'm reading Josuttis' C++20 the Complete Guide, and I think I've absorbed that a clause like the one in the template below
template
requires requires { typename std::remove_const_t; }
…
is completely useless as it resolves to…

Enlico
- 23,259
- 6
- 48
- 102
2
votes
1 answer
'requires' clause with fold expressions never satisfied
I'm struggling a lot to understand how concepts and contraints works. Until now I always managed to avoid them with type traits and static_assert or std::enable_if (or even SFINAE) but I want to reconcile with c++20 (well, for that part at least,…

Fareanor
- 5,900
- 2
- 11
- 37
2
votes
0 answers
requires clause on non-template friend function of a template class
Reproducible code as below or on godbolt compiles with clang trunk and MSVC but fails with gcc trunk. Given that non-template friend function of a template class could be constrained by requires clause, is it a gcc bug here?
template< typename…

wanghan02
- 1,227
- 7
- 14
1
vote
1 answer
Why does the cpp requires expression sometimes allow non valid expressions?
The following code is a simulation of an error I have in a larger code base. The idea is to collect various methods of calculating hashes of objects under one umbrella. The c++20 requires expression together with c++17 constexpr if provide a way to…

bradgonesurfing
- 30,949
- 17
- 114
- 217
1
vote
2 answers
Is clang right to complain about an out of line definition of a templated constructor using a concept?
Here is a struct with a templated constructor that is defined out of line:
template
struct Foo {
template
Foo(F f);
};
template
template
Foo::Foo(F f) {}
Clang is happy with this under…

jacobsa
- 5,719
- 1
- 28
- 60
1
vote
0 answers
When should I put a `requires` clause in a template function's signature instead of before it?
In C++20 you can add a requires clause (1) after a template parameter list as well as (2) after a function declaration (like a specifier). In the following snippet foo is an example of (1) and bar is an example of (2).
#include…

nebuch
- 6,475
- 4
- 20
- 39
-3
votes
2 answers
Does the `requires` nested in a requires-expression introduce a requires-clause?
From questions like this, from C++20 - The Complete Guide, and even from cppreference, my understanding is that the keyword requires can do one of 2 things only:
introduce a requires-clause
introduce a requires-expression
and I also think I've…

Enlico
- 23,259
- 6
- 48
- 102
-3
votes
1 answer
Clang accepts program involving requires-constraint while gcc and msvc rejects it
Both of the following programs are accepted by clang but rejected by gcc and msvc. Demo
#include
#include …

Jason
- 36,170
- 5
- 26
- 60