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: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 12.2.0
: P3 normal
Target Milestone: ---
Assignee: Jason Merrill
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2022-12-02 17:32 UTC by Ondřej Majerech
Modified: 2025-08-29 08:37 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2025-08-27 00:00:00


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 Drea Pinski 2022-12-02 20:28:37 UTC
I suspect this is the same issue as PR 57 .
Comment 2 Drea 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 Drea 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.
Comment 5 Ethan Hancock 2025-05-18 23:23:40 UTC
template<auto F>
struct Foo {};

Foo<[](){ return 1 >= 0; }> foo1{};

Foo<[](){ return (1 > 0); }> foo2{};

Foo<[](){ return 1 > 0; }> foo3{};


I ran afoul of this bug, and found this issue.
Can confirm that this bug is still present in GCC 15.1.0.
GCC accepts foo1 and foo2, but rejects foo3.

Clang does accept all 3.
Comment 6 GCC Commits 2025-08-28 15:50:42 UTC
The trunk branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:00e8690ef0d7ac1ced87ceee309dbfa27909a35c

commit r16-3444-g00e8690ef0d7ac1ced87ceee309dbfa27909a35c
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Aug 24 05:15:01 2025 -0400

    c++: > in lambda in template arg [PR107953]
    
    As with PR116928, we need to set greater_than_is_operator_p within the
    lambda delimiters.
    
            PR c++/107953
    
    gcc/cp/ChangeLog:
    
            * parser.cc (cp_parser_lambda_expression): Set
            greater_than_is_operator_p.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/lambda-targ18.C: New test.
Comment 7 Jason Merrill 2025-08-29 08:37:46 UTC
Fixed for GCC 16.