Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

concepts question

163 views
Skip to first unread message

Bonita Montero

unread,
Jun 9, 2021, 11:26:37 AM6/9/21
to
If you have a class / struct with a template-parameter that
is specified to have a concept-constraint with requires - why
it is necessary to re-define this dependency in the external
definition of all members ? Is there a deeper necessity in
this ?

template<typename T>
requires regular<T>
struct S
{
void f();
// ...
};

template<typename T>
requires regular<T> // why is this duplicate check necessary
void S<T>::f()
{
// ...
}

David Brown

unread,
Jun 9, 2021, 11:48:04 AM6/9/21
to
Are you sure it /is/ necessary? What sort of error message are you
getting from your compiler (and what compiler is it) ?
<https://godbolt.org> testing is always useful for that kind of thing.

"requires" adds restrictions on how a template can be used, thus giving
clearer error messages at an earlier stage. I don't see why they would
be needed in the implementation of a function if they are given in the
declaration.

Bonita Montero

unread,
Jun 9, 2021, 12:10:31 PM6/9/21
to
If I omit the commented requires-clause with MSVC I get the error:
error C3864: 'S<T>': requires clause is incompatible with the declaration
With clang I get the error:
error: requires clause differs in template redeclaration

David Brown

unread,
Jun 9, 2021, 12:50:38 PM6/9/21
to
I just tried it on <https://godbolt.org>, with the code you gave and the
two missing lines:

#include <concepts>
using namespace std;

Current gcc and clang with "--std=c++20 -Wpedantic" are happy with it,
as is MSVC v.19 with "/std:c++latest".

Are you missing some compiler options, or using a different version?
Again - go to <https://godbolt.org>, try it out on different compiler
versions and you might get an idea of what is going on.


Bonita Montero

unread,
Jun 9, 2021, 1:51:31 PM6/9/21
to
> I just tried it on <https://godbolt.org>, with the code you gave and the
> two missing lines:
>
> #include <concepts>
> using namespace std;
>
> Current gcc and clang with "--std=c++20 -Wpedantic" are happy with it,
> as is MSVC v.19 with "/std:c++latest".

clang 12 doesn't work: https://godbolt.org/z/jbzq8nP5G
g++ 11 does work.
-Wpedantic doesn't make a difference either.

David Brown

unread,
Jun 9, 2021, 2:49:03 PM6/9/21
to
You are right - somewhere between testing gcc (which does not complain)
and clang and MSVC, I had managed to undo the comment marker I had in
the second "requires". Sorry for the noise.

It seems it is only gcc that is happy with your code. It looked fine to
me, but I haven't studied the standards here.

0 new messages