-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
<memory>
: std::construct_at(s, " ")
did not evaluate to a constant
#2467
Comments
This is a clear compiler bug. You can play with other forms aka assignment etc that work. So it seems to be related to Strangely using a simple literal type works. Please file a developer community ticket |
This won't work in debug mode since we allocate memory in the default constructor of all containers and that memory isn't deallocated if you reuse the storage for the container without destruction. |
Correction: at compiletime the default constructor always allocates memory - we don't SSO in constant expressions. This is not a compiler bug. It may be a library bug? |
I doubt that this is the issue here. For once the error is quite different. Secondly it also fails when destroying the allocation with destroy_at |
This OP is simplified from the following, if it helps. #include<string>
#include<vector>
constexpr bool b = [] {
std::vector<std::string> v;
v.push_back(" "); // ok
v.emplace_back(" "); // not ok
return true;
}(); |
I doubt it's the only issue here.
Clang compiles successfully with the
It does, that example is orders of magnitude more motivating than the sample in the OP. |
Less reduced, but more library-free: #include <memory>
#include <string>
constexpr const char *space = " ";
static_assert([] {
std::string *v = std::allocator<std::string>{}.allocate(4);
std::construct_at(v + 0, std::string{}); // OK
std::construct_at(v + 1, space); // OK
std::construct_at(v + 2, " "); // note: a non-constant (sub-)expression was encountered
std::destroy_at(v + 2);
std::destroy_at(v + 1);
std::destroy_at(v + 0);
std::allocator<std::string>{}.deallocate(v, 4);
return true;
}()); // error C2131: expression did not evaluate to a constant |
I also checked with a local string_view of a whitespace, which also works. Seems that @cdacamar will have a lot of fun with all the string issues |
Luckily it has nothing to do with |
Removing all the library code I believe this is the minimal example namespace std {
template <class _Ty, class... _Types>
constexpr _Ty* construct_at(_Ty* _Location, _Types&&... _Args) {
return ::new (const_cast<void*>(static_cast<const volatile void*>(_Location))) _Ty(static_cast<_Types&&>(_Args)...);
}
}
struct meow {
constexpr meow() = default;
constexpr meow(const char*) {};
};
static_assert([] {
meow v[1];
std::construct_at(v, " ");
return true;
}()); |
Filed DevCom-1634430 |
Thanks for filing the compiler bug, @miscco! Since this is not an STL bug, and anyone interested can follow the compiler bug on DevCom, I'm closing this. |
https://godbolt.org/z/jdx7oGnMn
MSVC-trunk rejects it with:
I don't know if this is a language bug or a library bug, please correct me if I'm missing something.
The text was updated successfully, but these errors were encountered: