Bug 80488 - Erroneous error "lambda-expression in template argument"
Summary: Erroneous error "lambda-expression in template argument"
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: 13.2
Assignee: Patrick Palka
URL:
Keywords: c++-lambda, rejects-valid
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2017-04-22 01:10 UTC by Eric Niebler
Modified: 2023-05-12 15:24 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-04-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Eric Niebler 2017-04-22 01:10:56 UTC
Compile with -std=gnu++1z:

  template <auto> using x = void;
  using Y = x<+[]{}>;

The above code creates a lambda and then applies unary operator +, forcing it to decay to a function pointer. Since that's constexpr, it should be a valid template argument.

clang-trunk accepts this code.
Comment 1 Andreas Reischuck 2018-01-18 11:28:45 UTC
This bug also hit me.

I found this discussion on Stackoverflow: https://stackoverflow.com/questions/43213997/why-is-gcc-failing-when-using-lambda-for-non-type-template-parameter

And I can report that VS2017 15.5 and clang 6 can compile this code:

  template<void (*p)()>
  void x() {}

  void test() {
    x<[] {}>();
  }

template<auto> is not yet supported.
Comment 2 Andrew Pinski 2021-12-20 06:36:58 UTC
The first example works with -std=c++20 as expected (it is not valid C++17 and gets rejected with -std=c++1z too).

The second example is related to PR 83258 (or is a dup of that bug).
Comment 3 GCC Commits 2023-05-11 14:05:03 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:c3afdb8ba8f1839544c414f57e41a58c8fda5349

commit r14-708-gc3afdb8ba8f1839544c414f57e41a58c8fda5349
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu May 11 10:04:25 2023 -0400

    c++: converted lambda as template argument [PR83258, ...]
    
    r8-1253-g3d2e25a240c711 removed the template argument linkage requirement
    in convert_nontype_argument for C++17 (which r9-3836-g4be5c72cf3ea3e later
    factored out into invalid_tparm_referent_p), but we need to also remove
    the one in convert_nontype_argument_function for benefit of the first and
    third testcase which we currently reject even in C++17/20 mode.
    
    And in invalid_tparm_referent_p we're inadvertendly returning false for
    the address of a lambda's static op() since it's DECL_ARTIFICIAL, which
    currently causes us to reject the second (C++20) testcase.  But this
    DECL_ARTIFICIAL check seems to be relevant only for VAR_DECL, and in fact
    this code path was originally reachable only for VAR_DECL until recently
    (r13-6970-gb5e38b1c166357).  So this patch restricts the check to VAR_DECL.
    
    Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
    
            PR c++/83258
            PR c++/80488
            PR c++/97700
    
    gcc/cp/ChangeLog:
    
            * pt.cc (convert_nontype_argument_function): Remove linkage
            requirement for C++17 and later.
            (invalid_tparm_referent_p) <case ADDR_EXPR>: Restrict
            DECL_ARTIFICIAL rejection test to VAR_DECL.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/ext/visibility/anon8.C: Don't expect a "no linkage"
            error for the template argument &B2:fn in C++17 mode.
            * g++.dg/cpp0x/lambda/lambda-conv15.C: New test.
            * g++.dg/cpp2a/nontype-class56.C: New test.
            * g++.dg/template/function2.C: New test.
Comment 4 Patrick Palka 2023-05-11 14:26:02 UTC
Fixed on trunk so far.
Comment 5 GCC Commits 2023-05-12 15:08:01 UTC
The releases/gcc-13 branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:42f9b481be3527b336b800128247d053fd18d121

commit r13-7324-g42f9b481be3527b336b800128247d053fd18d121
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu May 11 10:04:25 2023 -0400

    c++: converted lambda as template argument [PR83258, ...]
    
    r8-1253-g3d2e25a240c711 removed the template argument linkage requirement
    in convert_nontype_argument for C++17 (which r9-3836-g4be5c72cf3ea3e later
    factored out into invalid_tparm_referent_p), but we need to also remove
    the one in convert_nontype_argument_function for benefit of the first and
    third testcase which we currently reject even in C++17/20 mode.
    
    And in invalid_tparm_referent_p we're inadvertendly returning false for
    the address of a lambda's static op() since it's DECL_ARTIFICIAL, which
    currently causes us to reject the second (C++20) testcase.  But this
    DECL_ARTIFICIAL check seems to be relevant only for VAR_DECL, and in fact
    this code path was originally reachable only for VAR_DECL until recently
    (r13-6970-gb5e38b1c166357).  So this patch restricts the check to VAR_DECL.
    
    Co-authored-by: Jonathan Wakely <jwakely@redhat.com>
    
            PR c++/83258
            PR c++/80488
            PR c++/97700
    
    gcc/cp/ChangeLog:
    
            * pt.cc (convert_nontype_argument_function): Remove linkage
            requirement for C++17 and later.
            (invalid_tparm_referent_p) <case ADDR_EXPR>: Restrict
            DECL_ARTIFICIAL rejection test to VAR_DECL.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/ext/visibility/anon8.C: Don't expect a "no linkage"
            error for the template argument &B2:fn in C++17 mode.
            * g++.dg/cpp0x/lambda/lambda-conv15.C: New test.
            * g++.dg/cpp2a/nontype-class56.C: New test.
            * g++.dg/template/function2.C: New test.
    
    (cherry picked from commit c3afdb8ba8f1839544c414f57e41a58c8fda5349)
Comment 6 Patrick Palka 2023-05-12 15:24:49 UTC
Should be fully fixed for GCC 13.2