Cannot use consteval functions inside initializer list on MSVC
Asked Answered
T

1

15

Consider that minimized code snippet:

#include <vector>

class Bar
{
public:
    constexpr Bar() {}
};

consteval Bar foo()
{
    return Bar();
}

int main()
{
    std::vector<Bar> bars{ foo(), foo() };
}

This doesn't compile on latest MSVC compiler (Visual Studio 2022 version 17.3.3), but does on Clang or GCC.

Compiler Explorer

Is the code somewhere ill formed, or it is a bug in MSVC?

Tamas answered 12/9, 2022 at 10:46 Comment(8)
I think (but I'm not positive) that since you are using an STL container (vector) to store a custom type (Bar), the problem is that no copy (or move) ctor is defined (the call foo() returns an rvalue).Fourthclass
@RonaldSouza Possibly, but I doubt, since my original failing code was not using a STL container, and original Bar class was copyable.Tamas
@RonaldSouza - Those operations are implicitly defined. There is nothing here to supress their generation.Koine
Duplicate? Different behavior of consteval in GCC and MSVC (not work)Deodorant
I'm 99% certain it's a gap in MSVC's implementation. Not near a PC to verify comfortably, though...Koine
... the answer to the suggested dupe has a link to an MSDN blog that claims the issue is fixed, though. Seems to be incorrect.Deodorant
@StoryTeller-UnslanderMonica (and prapin): I see. Good point.Fourthclass
@AdrianMole Maybe the same original bug source, but it seems that current MSVC compiles the snippets in linked question, so not completely a duplicate.Tamas
T
10

From the comments, it looks like this is indeed a bug in MSVC.

Therefore I filled in a bug report on Visual Studio Community.

https://developercommunity.visualstudio.com/t/Cannot-use-consteval-functions-returning/10145209

Tamas answered 12/9, 2022 at 13:6 Comment(1)
In my Visual Studio 2022, gave compile error error C7595: 'foo': call to immediate function is not a constant expressionFrants

© 2022 - 2024 — McMap. All rights reserved.