When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
29 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
Arguments of a function are never considered to be constexpr inside of the function
Even if the function is running at compile-time
Yet the return value of that function can be constexpr for its caller
The only way this can work is by making the function consteval, and instead of using a static_assert, failing in some other way, e.g. by throwing
I was literally about to ask if this was the case with consteval
Typically (e.g. in std::format) they do this by wrapping the format string in a class like format_string<Args...> and giving that a constexpr consteval constructor from a string, and validating the format string in that constructor
So the rest of the format() function doesn't need to be consteval
It's the case with consteval too
This has always confused me, but I believe the reason it's like this is that the compiler developers don't want things that are not templates to work like templates
It means it successfully detected the format string error. If the throw wasn't actually reached, it would compile
I mean a template would generate a whole new function from a function template while a constexp parameter would just be a constant expression at compile time
Imagine like
auto foo(std::size_t n)
{
return std::array<int, n>{};
}
Yeah, but if you make the params constexpr, you suddenly have types in your function that can depend on those parameters
Essentially turning it into a template if it was allowed
they would not generate code, which is the whole point of a template
Yeah, but still having the types depend on values would be wild
consteval constructor, otherwise it doesn't work
Thanks for catching that
you'd have now a function that returns different types for different inputs
normally in C++ you know from identity of a function and the argument types what the result type will be, not sure you'd want to drop that
Yep
Ah I understand
yeah that makes sense.
This question is being automatically marked as stale.
If your question has been answered, type !solved.
If your question is not answered feel free to bump the post or re-ask.
Take a look at !howto ask for tips on improving your question.