https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112642

--- Comment #5 from Miro Palmu <miro.palmu at helsinki dot fi> ---
I have been trying to figure out where exactly the bug is and these are my
findings.
> Or:
>
> #include <string>
>
> consteval void bar() {
>     auto _ = [](std::string s) { return s; }({});
> }
>
> int main() {
>    bar();    
> }
>
> Or:
> 
> #include <string>
> constexpr auto foo(std::string init) { return init; }
> constexpr auto bar() { return foo("").size(); }
> constexpr auto i = bar();
>
>Clang compiles both of these without problems (it can't compile anything using 
>""s in a constant expression, maybe due to 
>https://github.com/llvm/llvm-project/issues/68527)
>
> So I am pretty sure this is a g++ front end bug.

If you use libstdc++ on clang these will not compile but with different errors.

Then with following example I try to showcase the bug without std::string.
Try it out: https://godbolt.org/z/rvoeMEaxc

This is bare minimum of  libstdc++ basic_string to reproduce this bug:

---

struct S {
    union {
        char a[1];
    };
    char* ptr; 
    constexpr S() : ptr{a} {
        a[0] = {};
    }
    constexpr S(S&&) = delete;
    constexpr S(const S&) = delete;
    constexpr S operator=(S&&) = delete;
    constexpr S operator=(const S&) = delete;
    constexpr ~S() = default;
}

---

Then to reproduce the bug instance of this class has to be function parameter
and the function has to be constant evaluated.
This can happens in std::basic_string move constructor bits/basic_string.h:682
and following tester functions tries to emulate what happens in it.

---

// Should always be false
constexpr bool test(const S& s){
    return s.ptr != s.a;
}
consteval void tester1(S param = {}) { 
    S notparam = {};
    if (test(notparam)){
        throw std::logic_error("compiletime notparam!");
    }

    if (test(param)) {
        // gcc ends up here so fails to compile
        // in std::basic_string move constructor
        // compilation would fail due to accessing
        // inactive union member
        // clang and msvc never end up here

        throw std::logic_error("compiletime param");
    }
}

int main() { tester(); )

---

Notice that here only the parameter version fails.
In non-constant evaluated context (see godbolt link)
all of the test evaluate false as they should.

Reply via email to