Bug 107953 - Greater-than operator misparsed inside a lambda expression used as a template argument
Summary: Greater-than operator misparsed inside a lambda expression used as a template...
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.2.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2022-12-02 17:32 UTC by Ondřej Majerech
Modified: 2022-12-02 23:55 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ondřej Majerech 2022-12-02 17:32:08 UTC
template <auto>
void
f() { }

constexpr auto g = f<[] (int x, int y) { return x > y; }>;

This produces the following diagnostic:
<source>: In lambda function:
<source>:5:50: error: expected ';' before '>' token
    5 | constexpr auto g = f<[] (int x, int y) { return x > y; }>;
      |                                                  ^~
      |                                                  ;
<source>:5:51: error: expected primary-expression before '>' token
    5 | constexpr auto g = f<[] (int x, int y) { return x > y; }>;
      |                                                   ^
ASM generation compiler returned: 1
<source>: In lambda function:
<source>:5:50: error: expected ';' before '>' token
    5 | constexpr auto g = f<[] (int x, int y) { return x > y; }>;
      |                                                  ^~
      |                                                  ;
<source>:5:51: error: expected primary-expression before '>' token
    5 | constexpr auto g = f<[] (int x, int y) { return x > y; }>;
      |                                                   ^

Godbolt link to the above: https://godbolt.org/z/KG6d5E6ev

Changing the body to return (x > y); makes the error go away as a workaround. So I speculate that the greater-than operator is misparsed as the end of the template argument list.
Comment 1 Andrew Pinski 2022-12-02 20:28:37 UTC
I suspect this is the same issue as PR 57 .
Comment 2 Andrew Pinski 2022-12-02 23:18:57 UTC
Even template defaults has issues:
template <auto = [] (int x, int y) { return x > y; }>
int t = 0;

Which is why I Pointed to PR 57.
Comment 3 Andrew Pinski 2022-12-02 23:23:23 UTC
I wonder if this is still an ambiguous part of the C++ grammar and all.
Comment 4 Ondřej Majerech 2022-12-02 23:55:03 UTC
It seems that the core of PR 57 is that `A<T` could syntactically be a valid expression, and it would require a look-up to determine that A and T are in fact a template and a type, and not values.

Here, however, the fragment `[] (int x, int y) { return x` doesn't form a syntactically valid expression. So, to me at least, it seems there should be no ambiguity.