Skip to content

if constexpr appears to not work correctly in some nested templates #131080

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

Closed
HolyBlackCat opened this issue Mar 13, 2025 · 3 comments
Closed

if constexpr appears to not work correctly in some nested templates #131080

HolyBlackCat opened this issue Mar 13, 2025 · 3 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate lambda C++11 lambda expressions

Comments

@HolyBlackCat
Copy link

I think I've seen similar tickets at some point, so this could be a duplicate.

The following code doesn't compile, but it should: https://gcc.godbolt.org/z/648eY8eh5

template <typename T>
struct FalsePredicate
{
    static constexpr bool value = false;
};

template <typename T, template <typename> typename Cond>
void foo()
{
    []<template <typename> typename Cond2 = Cond>
    {
        if constexpr (Cond2<T>::value)
            T::foo();
    }();
}

int main()
{
    foo<int, FalsePredicate>();
}

It says that T::foo() is invalid for T=int, despite there being if constexpr.

Tested on Clang 20 and trunk.

@frederick-vs-ja
Copy link
Contributor

IIUC this is P0588R1. Closing as duplicate of its tracking issue.

@frederick-vs-ja frederick-vs-ja added duplicate Resolved as duplicate clang:frontend Language frontend issues, e.g. anything involving "Sema" lambda C++11 lambda expressions and removed new issue labels Mar 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 13, 2025

@llvm/issue-subscribers-clang-frontend

Author: Egor (HolyBlackCat)

I think I've seen similar tickets at some point, so this could be a duplicate.

The following code doesn't compile, but it should: https://gcc.godbolt.org/z/648eY8eh5

template &lt;typename T&gt;
struct FalsePredicate
{
    static constexpr bool value = false;
};

template &lt;typename T, template &lt;typename&gt; typename Cond&gt;
void foo()
{
    []&lt;template &lt;typename&gt; typename Cond2 = Cond&gt;
    {
        if constexpr (Cond2&lt;T&gt;::value)
            T::foo();
    }();
}

int main()
{
    foo&lt;int, FalsePredicate&gt;();
}

It says that T::foo() is invalid for T=int, despite there being if constexpr.

Tested on Clang 20 and trunk.

@HolyBlackCat
Copy link
Author

HolyBlackCat commented Mar 13, 2025

@frederick-vs-ja From the first glance that seems unrelated, as I don't use captures in this example. I think I get it. 🙂

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" duplicate Resolved as duplicate lambda C++11 lambda expressions
Projects
None yet
Development

No branches or pull requests

3 participants