r/cpp • u/Massive-Bottle-5394 • Mar 31 '26
std::constant_wrapper acts unexpectedly
I'm experimenting with reflection in C++26 and other things. And I'm confused by the behavior of std::constant_wrapper. https://godbolt.org/z/vo6rbMb41
Can somebody explain why example not working and how to fix?
I need to implement some reflection functions using this technique (deducing a template parameter via a constructor argument), but this error is making me wonder
EDIT
I did a little research, and it seems that this behavior comes from the implementation in GCC, which for some reason doesn't follow the wording of the proposal that literally describes the entire implementation
EDIT 2
I was wrong. There is a newer version of the proposal that explains the observed behavior. Still, it’s a sad thing that my case isn’t supported.
View all comments
Show parent comment
1
u/friedkeenan Apr 01 '26
There could maybe be something more limited where you could have a constexpr parameter that you could use in the function body but that can't affect the function signature, see Barry's recent blogpost or my own post, both of which can get a function parameter lifted into something you can eventually use as a template parameter.
I'm unsure of the technical details of what would be needed to add more ergonomic support, but that more scoped behavior is at least possible today in C++26, with meaningful caveats. But maybe there could be more work done in that direction?
As for the general case of using a constexpr parameter like any other template parameter, I'll take your word for it that it's impossible with C++ how it is.