Skip to content

Reference argument specified as template can't be used in constexpr if #129573

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
abellgithub opened this issue Mar 3, 2025 · 4 comments
Open
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation

Comments

@abellgithub
Copy link

Perhaps a duplicate of #63139

The following fails to compile with 19.1.0 and current trunk. It works if the argument is a value type instead of reference. Works with GCC.

class Foo
{
public:
    constexpr static bool foo = true;
};

template <typename T>
int func(T& t)
{
    if constexpr (t.foo)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{
    Foo f;

    int i = func(f);
    return i;
}
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation and removed new issue labels Mar 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 3, 2025

@llvm/issue-subscribers-clang-frontend

Author: Andrew Bell (abellgithub)

Perhaps a duplicate of #63139

The following fails to compile with 19.1.0 and current trunk. It works if the argument is a value type instead of reference. Works with GCC.

class Foo
{
public:
    constexpr static bool foo = true;
};

template &lt;typename T&gt;
int func(T&amp; t)
{
    if constexpr (t.foo)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

int main()
{
    Foo f;

    int i = func(f);
    return i;
}

@frederick-vs-ja
Copy link
Contributor

FWIW, Clang accepts this since ver. 20 but only in C++23 and later modes (Godbolt link).

As P2280R4 is adopted as a defect report, I think Clang should apply it to C++11 and later, at least after some resolving some bugs.

@tbaederr
Copy link
Contributor

tbaederr commented Mar 4, 2025

at least after some resolving some bugs.

Like what? Any known problems other than those constexpr-unknown values making it into codegen?

@frederick-vs-ja
Copy link
Contributor

at least after some resolving some bugs.

Like what? Any known problems other than those constexpr-unknown values making it into codegen?

I didn't found any. Hopefully that is the only bug, and we will be able to start backporting after resolving 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" constexpr Anything related to constant evaluation
Projects
None yet
Development

No branches or pull requests

5 participants