Skip to content

Default non-type template parameters with immediately invoked lambdas fail with deduction guides #51315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
friedkeenan opened this issue Sep 26, 2021 · 4 comments
Labels
bugzilla Issues migrated from bugzilla c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts confirmed Verified by a second party lambda C++11 lambda expressions

Comments

@friedkeenan
Copy link

friedkeenan commented Sep 26, 2021

Bugzilla Link 51973
Version trunk
OS Linux
CC @DougGregor,@friedkeenan,@zygoloid

Extended Description

With the following code, clang claims the defaulted non-type template parameter is not a constant expression, despite it being so (with GCC handling it fine as well):

#include <concepts>

template<
    typename T,
    auto V = []() {
        if constexpr (std::same_as<T, int>) {
            return 1;
        } else {
            return 2;
        }
    }()
>
struct Test {
    constexpr Test(T) { }
};

template<typename T>
Test(T) -> Test<T>;

void test() {
    Test<int, 1>(1);
}

Godbolt link: https://godbolt.org/z/jT3zPx9fx

As you can see, even if you specify the template parameters when constructing, it still complains, and if you duplicate the immediately invoked lambda in the deduction guide and remove the explicated template arguments for the constructor, clang actually says that it can't compile that expression yet. However, if you specify the template arguments, it consumes everything fine.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 11, 2021
@Endilll Endilll added the clang:to-be-triaged Should not be used for new issues label Jul 18, 2024
@Endilll Endilll added c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang:to-be-triaged Should not be used for new issues c++ labels Mar 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 10, 2025

@llvm/issue-subscribers-clang-frontend

Author: None (friedkeenan)

| | | | --- | --- | | Bugzilla Link | [51973](https://llvm.org/bz51973) | | Version | trunk | | OS | Linux | | CC | @DougGregor,@friedkeenan,@zygoloid |

Extended Description

With the following code, clang claims the defaulted non-type template parameter is not a constant expression, despite it being so (with GCC handling it fine as well):

#include &lt;concepts&gt;

template&lt;
    typename T,
    auto V = []() {
        if constexpr (std::same_as&lt;T, int&gt;) {
            return 1;
        } else {
            return 2;
        }
    }()
&gt;
struct Test {
    constexpr Test(T) { }
};

template&lt;typename T&gt;
Test(T) -&gt; Test&lt;T&gt;;

void test() {
    Test&lt;int, 1&gt;(1);
}

Godbolt link: https://godbolt.org/z/jT3zPx9fx

As you can see, even if you specify the template parameters when constructing, it still complains, and if you duplicate the immediately invoked lambda in the deduction guide and remove the explicated template arguments for the constructor, clang actually says that it can't compile that expression yet. However, if you specify the template arguments, it consumes everything fine.

@llvmbot
Copy link
Member

llvmbot commented Mar 10, 2025

@llvm/issue-subscribers-c-20

Author: None (friedkeenan)

| | | | --- | --- | | Bugzilla Link | [51973](https://llvm.org/bz51973) | | Version | trunk | | OS | Linux | | CC | @DougGregor,@friedkeenan,@zygoloid |

Extended Description

With the following code, clang claims the defaulted non-type template parameter is not a constant expression, despite it being so (with GCC handling it fine as well):

#include &lt;concepts&gt;

template&lt;
    typename T,
    auto V = []() {
        if constexpr (std::same_as&lt;T, int&gt;) {
            return 1;
        } else {
            return 2;
        }
    }()
&gt;
struct Test {
    constexpr Test(T) { }
};

template&lt;typename T&gt;
Test(T) -&gt; Test&lt;T&gt;;

void test() {
    Test&lt;int, 1&gt;(1);
}

Godbolt link: https://godbolt.org/z/jT3zPx9fx

As you can see, even if you specify the template parameters when constructing, it still complains, and if you duplicate the immediately invoked lambda in the deduction guide and remove the explicated template arguments for the constructor, clang actually says that it can't compile that expression yet. However, if you specify the template arguments, it consumes everything fine.

@EugeneZelenko EugeneZelenko added concepts C++20 concepts lambda C++11 lambda expressions labels Mar 10, 2025
@shafik shafik added the confirmed Verified by a second party label Mar 14, 2025
@shafik
Copy link
Collaborator

shafik commented Mar 14, 2025

Still does not work: https://godbolt.org/z/85Mj487hv

CC @cor3ntin

@zyn0217
Copy link
Contributor

zyn0217 commented Mar 14, 2025

The dependency of lambda doesn't seem right

https://godbolt.org/z/6KdT4Tj7Y

we crash w/ an assertion enabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" concepts C++20 concepts confirmed Verified by a second party lambda C++11 lambda expressions
Projects
Status: No status
Development

No branches or pull requests

6 participants