Skip to content
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

Fail to consider a member access from an object to a static member a constant expression #57254

Closed
Sainan opened this issue Aug 19, 2022 · 11 comments
Assignees
Labels
c++11 clang:frontend Language frontend issues, e.g. anything involving "Sema" consteval C++20 consteval duplicate Resolved as duplicate

Comments

@Sainan
Copy link

Sainan commented Aug 19, 2022

#include <iostream>

struct Usertype
{
    [[nodiscard]] static constexpr bool getSomething() noexcept
    {
        return true;
    }
};

void checkSomething(Usertype& ud)
{
    if constexpr (ud.getSomething()) // on linux, only works if ud is not a reference or r-value
    //if constexpr (std::remove_reference_t<decltype(ud)>::getSomething()) // always works
    {
        std::cout << "Alright!\n";
    }
}

int main()
{
    Usertype ud;
    checkSomething(ud);
}

This compiles fine on Windows, but on Linux I get "constexpr if condition is not a constant expression".

Tested against clang versions:

  • Windows: 14.0.5, 13.0.0
  • Linux: 14.0.0, 13.0.1, 13.0.0, 10.0.0-4ubuntu1, 7.0.1-8+deb10u2

Using C++17 in all instances.

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

llvmbot commented Aug 19, 2022

@llvm/issue-subscribers-clang-frontend

@tbaederr
Copy link
Contributor

Using Usertype::getSomething() works.

@Sainan
Copy link
Author

Sainan commented Aug 20, 2022

Right, I was looking at this from the perspective of the type being hidden due to a macro that defines a template function. In any case, I have workarounds, it's not a show-stopper of a problem, but just really intriguing that the build only fails on Linux.

@tbaederr
Copy link
Contributor

the type being hidden due to a macro that defines a template function.

Wait, where's a macro involved? And where a template function?

I was thinking it might be due to a different stdlib, but

struct Usertype
{
    static constexpr bool getSomething()
    {
        return true;
    }
};

void checkSomething(Usertype& ud)
{
    if constexpr (ud.getSomething()) {}
}

Also reproduces.

@Sainan
Copy link
Author

Sainan commented Aug 20, 2022

Wait, where's a macro involved? And where a template function?

Not in the example, but just explaining how I was looking at it that I didn't think of Usertype::getSomething().

@AaronBallman AaronBallman changed the title Platform-dependent constexpr Fail to consider a member access from an object to a static member a constant expression Aug 23, 2022
@AaronBallman
Copy link
Collaborator

This looks very closely related to #56246 -- that one is about resolving to a static data member of this, but this one is through a member object expression other than this.

@usx95 usx95 added consteval C++20 consteval c++11 labels Aug 23, 2022
@llvmbot
Copy link
Collaborator

llvmbot commented Aug 23, 2022

@llvm/issue-subscribers-c-11

@usx95
Copy link
Contributor

usx95 commented Aug 23, 2022

struct Foo {
    static constexpr bool Bar() { return true; }
};

void func(Foo& foo) { 
    static_assert(foo.Bar());
}

https://godbolt.org/z/4add9zche
foo.Bar() is not a constant expression because foo's lifetime began outside the expression and it is not initialised with a constant expression.
http://eel.is/c++draft/expr.const#4

Related discussions: #40438
Related Patch: 00068c4#diff-24549b124046a492279fe53ffe2aca86550f281b025a48067c043a1c944547faR2306-R2323

Can you confirm @AaronBallman

@AaronBallman
Copy link
Collaborator

Can you confirm @AaronBallman

I think this is the subject of https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2280r4.html which we don't claim to implement yet. (Thanks @shafik for noticing that connection!)

@shafik shafik self-assigned this Feb 6, 2023
@cor3ntin
Copy link
Contributor

cor3ntin commented Jun 6, 2023

Should be fixed by #63139

@zygoloid
Copy link
Collaborator

zygoloid commented Jun 6, 2023

Closing as duplicate of #63139.

@zygoloid zygoloid closed this as not planned Won't fix, can't repro, duplicate, stale Jun 6, 2023
@EugeneZelenko EugeneZelenko added the duplicate Resolved as duplicate label Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++11 clang:frontend Language frontend issues, e.g. anything involving "Sema" consteval C++20 consteval duplicate Resolved as duplicate
Projects
Status: Done
Development

No branches or pull requests

9 participants