public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* fail with the following concept code
@ 2023-02-28 21:21 Watson Romero
  2023-02-28 21:25 ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Watson Romero @ 2023-02-28 21:21 UTC (permalink / raw)
  To: gcc-help

[-- Attachment #1: Type: text/plain, Size: 1556 bytes --]

I was trying to learn how concepts work and for some reason the following
lines of code:

#include <cstddef>
#include <cstdio>
#include <type_traits>

template <typename T>
concept Averageable = std::is_default_constructible<T>::value &&
        std::is_copy_constructible<T>::value &&
        requires(T a, T b) {
        { a + b }->T;
        { a / b }->T;
  };
}

template <Averageable T>
T mean(const T* values, size_t length) {
  T result{};
  for(size_t i{}; i < length; i++) {
    result += values[i];
  }
  return result / length;
}

int main() {
  const double nums_d[]{ 1.0f, 2.0f, 3.0f, 4.0f };
  const auto result1 = mean(nums_d, 4);
  printf("double: %f\n", result1);

  const float nums_f[]{ 1.0, 2.0, 3.0, 4.0 };
  const auto result2 = mean(nums_f, 4);
  printf("float: %f\n", result2);

  const char nums_c[]{ 1, 2, 3, 4 };
  const auto result3 = mean(nums_c, 4);
  printf("char: %d\n", result3);
}

produces the following error while running g++ like so:
g++ listing_6_21.cpp -std=c++20 -o a.out


listing_6_21.cpp:9:20: error: return-type-requirement is not a
type-constraint
    9 |         { a + b }->T;
      |                    ^
listing_6_21.cpp:10:20: error: return-type-requirement is not a
type-constraint
   10 |         { a / b }->T;
      |                    ^
listing_6_21.cpp:12:1: error: expected declaration before ‘}’ token
   12 | }

I'm not sure if this is consistent with what should happen but I figured
I'd inform you guys about it.

Sincerely,
Watson

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: fail with the following concept code
  2023-02-28 21:21 fail with the following concept code Watson Romero
@ 2023-02-28 21:25 ` Jonathan Wakely
  2023-02-28 22:16   ` Watson Romero
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2023-02-28 21:25 UTC (permalink / raw)
  To: Watson Romero; +Cc: gcc-help

On Tue, 28 Feb 2023 at 21:22, Watson Romero via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> I was trying to learn how concepts work and for some reason the following
> lines of code:
>
> #include <cstddef>
> #include <cstdio>
> #include <type_traits>
>
> template <typename T>
> concept Averageable = std::is_default_constructible<T>::value &&
>         std::is_copy_constructible<T>::value &&
>         requires(T a, T b) {
>         { a + b }->T;
>         { a / b }->T;
>   };
> }
>
> template <Averageable T>
> T mean(const T* values, size_t length) {
>   T result{};
>   for(size_t i{}; i < length; i++) {
>     result += values[i];
>   }
>   return result / length;
> }
>
> int main() {
>   const double nums_d[]{ 1.0f, 2.0f, 3.0f, 4.0f };
>   const auto result1 = mean(nums_d, 4);
>   printf("double: %f\n", result1);
>
>   const float nums_f[]{ 1.0, 2.0, 3.0, 4.0 };
>   const auto result2 = mean(nums_f, 4);
>   printf("float: %f\n", result2);
>
>   const char nums_c[]{ 1, 2, 3, 4 };
>   const auto result3 = mean(nums_c, 4);
>   printf("char: %d\n", result3);
> }
>
> produces the following error while running g++ like so:
> g++ listing_6_21.cpp -std=c++20 -o a.out
>
>
> listing_6_21.cpp:9:20: error: return-type-requirement is not a
> type-constraint
>     9 |         { a + b }->T;
>       |                    ^
> listing_6_21.cpp:10:20: error: return-type-requirement is not a
> type-constraint
>    10 |         { a / b }->T;
>       |                    ^
> listing_6_21.cpp:12:1: error: expected declaration before ‘}’ token
>    12 | }
>
> I'm not sure if this is consistent with what should happen but I figured
> I'd inform you guys about it.

This is the expected, correct behaviour for a C++20 compiler.

The Concepts TS used the syntax in your code, but in C++20 you must
use a type-constraint, like so:

{ a + b } -> std::is_same_v<T>;

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: fail with the following concept code
  2023-02-28 21:25 ` Jonathan Wakely
@ 2023-02-28 22:16   ` Watson Romero
  0 siblings, 0 replies; 3+ messages in thread
From: Watson Romero @ 2023-02-28 22:16 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

[-- Attachment #1: Type: text/plain, Size: 2168 bytes --]

Got it, thanks!

On Tue, Feb 28, 2023 at 4:26 PM Jonathan Wakely <jwakely.gcc@gmail.com>
wrote:

> On Tue, 28 Feb 2023 at 21:22, Watson Romero via Gcc-help
> <gcc-help@gcc.gnu.org> wrote:
> >
> > I was trying to learn how concepts work and for some reason the following
> > lines of code:
> >
> > #include <cstddef>
> > #include <cstdio>
> > #include <type_traits>
> >
> > template <typename T>
> > concept Averageable = std::is_default_constructible<T>::value &&
> >         std::is_copy_constructible<T>::value &&
> >         requires(T a, T b) {
> >         { a + b }->T;
> >         { a / b }->T;
> >   };
> > }
> >
> > template <Averageable T>
> > T mean(const T* values, size_t length) {
> >   T result{};
> >   for(size_t i{}; i < length; i++) {
> >     result += values[i];
> >   }
> >   return result / length;
> > }
> >
> > int main() {
> >   const double nums_d[]{ 1.0f, 2.0f, 3.0f, 4.0f };
> >   const auto result1 = mean(nums_d, 4);
> >   printf("double: %f\n", result1);
> >
> >   const float nums_f[]{ 1.0, 2.0, 3.0, 4.0 };
> >   const auto result2 = mean(nums_f, 4);
> >   printf("float: %f\n", result2);
> >
> >   const char nums_c[]{ 1, 2, 3, 4 };
> >   const auto result3 = mean(nums_c, 4);
> >   printf("char: %d\n", result3);
> > }
> >
> > produces the following error while running g++ like so:
> > g++ listing_6_21.cpp -std=c++20 -o a.out
> >
> >
> > listing_6_21.cpp:9:20: error: return-type-requirement is not a
> > type-constraint
> >     9 |         { a + b }->T;
> >       |                    ^
> > listing_6_21.cpp:10:20: error: return-type-requirement is not a
> > type-constraint
> >    10 |         { a / b }->T;
> >       |                    ^
> > listing_6_21.cpp:12:1: error: expected declaration before ‘}’ token
> >    12 | }
> >
> > I'm not sure if this is consistent with what should happen but I figured
> > I'd inform you guys about it.
>
> This is the expected, correct behaviour for a C++20 compiler.
>
> The Concepts TS used the syntax in your code, but in C++20 you must
> use a type-constraint, like so:
>
> { a + b } -> std::is_same_v<T>;
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-28 22:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-28 21:21 fail with the following concept code Watson Romero
2023-02-28 21:25 ` Jonathan Wakely
2023-02-28 22:16   ` Watson Romero

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).