Bug 80242 - [C++17+] "Trailing return types" with "non-type template arguments" which could be "constant expressions" produce a parsing error
Summary: [C++17+] "Trailing return types" with "non-type template arguments" which cou...
Status: RESOLVED DUPLICATE of bug 68495
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 7.0.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2017-03-28 20:39 UTC by Vittorio Romeo
Modified: 2023-06-23 13:26 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-12 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vittorio Romeo 2017-03-28 20:39:12 UTC
Consider the following snippet:

    template <int>
    struct bar {};

    template <class I>
    auto foo(I i) -> bar<i()> { return {}; }

    int main()
    {
        foo([]{ return 1; }); // (0)
    }

This compiles and works as intended on clang++5, but produces a compile-time error on g++7:

    prog.cc:5:25: error: template argument 1 is invalid
    auto foo(I i) -> bar<i()> { return {}; }
                            ^
Live example:
https://wandbox.org/permlink/6cbnRiTyphIaf2yh

--

I think this is a bug because:

* `[]{ return 1; }` is implicitly `constexpr`, as `1` is a "core constant expression".

* In the context of the function call marked with "(0)", the argument `i` is a closure generated by the lambda expression `[]{ return 1; }`. The closure has a `constexpr operator()` overload which returns a `1` (a "core constant expression").

* `bar<i()>` is a valid "trailing return type", as `i()` can be a "core constant expression" if it results in a `constexpr` function invocation.

---

Additional info, context and examples:
http://stackoverflow.com/questions/43043397
Comment 1 Andrew Pinski 2021-08-12 01:31:14 UTC
Confirmed for C++17+.  lambdas are only constexpr in C++17+.
Comment 2 Roland Schulz 2023-06-22 19:44:02 UTC
Is https://stackoverflow.com/questions/76534613/ a duplicate of this?
Comment 3 Patrick Palka 2023-06-23 13:26:41 UTC
(In reply to Roland Schulz from comment #2)
> Is https://stackoverflow.com/questions/76534613/ a duplicate of this?

Yes, this is in turn a dup of the older PR68495 I think

*** This bug has been marked as a duplicate of bug 68495 ***