Skip to content

Alias template with unevaluated non-generic lambda with constexpr if statements complains about mismatched returns when used in a dependent context #105886

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
MitalAshok opened this issue Aug 23, 2024 · 1 comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"

Comments

@MitalAshok
Copy link
Contributor

Maybe related to #58872

https://godbolt.org/z/dGT5Y8s9v

template<bool B>
using choose = decltype([] {
  if constexpr (B) {
    return 0;
  } else {
    return 0u;
  }
}());

static_assert(__is_same(choose<true>, int));
static_assert(__is_same(choose<false>, unsigned));

template<bool B>
choose<B> dependent;
<source>:6:5: error: return type 'unsigned int' must match previous return type 'int' when lambda expression has unspecified explicit return type
    6 |     return 0u;
      |     ^
<source>:2:28: note: while substituting into a lambda expression here
    2 | using choose = decltype([] {
      |                            ^
<source>:14:1: note: in instantiation of template type alias 'choose' requested here
   14 | choose<B> dependent;
      | ^

GCC and MSVC (after __is_same->std::is_same_v) are able to compile this.

It's fine when B isn't dependent. It's also fine if choose<B> is replaced with decltype([]{ ... }()).

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed new issue labels Aug 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 23, 2024

@llvm/issue-subscribers-clang-frontend

Author: Mital Ashok (MitalAshok)

Maybe related to #58872

https://godbolt.org/z/dGT5Y8s9v

template&lt;bool B&gt;
using choose = decltype([] {
  if constexpr (B) {
    return 0;
  } else {
    return 0u;
  }
}());

static_assert(__is_same(choose&lt;true&gt;, int));
static_assert(__is_same(choose&lt;false&gt;, unsigned));

template&lt;bool B&gt;
choose&lt;B&gt; dependent;
&lt;source&gt;:6:5: error: return type 'unsigned int' must match previous return type 'int' when lambda expression has unspecified explicit return type
    6 |     return 0u;
      |     ^
&lt;source&gt;:2:28: note: while substituting into a lambda expression here
    2 | using choose = decltype([] {
      |                            ^
&lt;source&gt;:14:1: note: in instantiation of template type alias 'choose' requested here
   14 | choose&lt;B&gt; dependent;
      | ^

GCC and MSVC (after __is_same->std::is_same_v) are able to compile this.

It's fine when B isn't dependent. It's also fine if choose&lt;B&gt; is replaced with decltype([]{ ... }()).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema"
Projects
None yet
Development

No branches or pull requests

3 participants