Date: Thu, 26 Nov 2026 12:25:16 +0100
On 26/11/2026 11:35, Miguel Veganzones wrote:
> What would be the difference with a normal template parameter other than
> the callsite?
>
It would be different at the call site - the call would look more like a
normal function. And it would be different at the definition site,
giving a more abbreviated template structure to the code.
But it would not allow anything that you could not do from before - it
would just make things a little neater (IMHO).
> I am not too familiar with std::cw, but it seems to solve a common issue
> for me: a function can be implemented with a compile time size or a
> runtime size, but compile time information is preferable if available.
> Such a function could accept 'auto size', check if this value is a
> constant literal, and specialize accordingly. In such scenario the
> caller can provide runtime or compile time information to the callee
> without knowing it itself wether this information is usable at compile time.
>
Sure. The suggestion would not negate all use-cases for std::cw. In
particular, a function that has a constexpr parameter could only be
called with a constant expression argument - just like a non-type
template argument. The use of std::cw<> can let you have functions that
can take either constant expressions or value expressions, with a
definition that can have "if constexpr" checks to distinguish them. If
you wanted to achieve that with constexpr parameters, you'd have to have
two functions - one with a constexpr parameter, the other with no
"constexpr" on the parameter. (Such overloading resolution would be a
complication in making this feature, but I think it should be resolvable.)
David
> Just my two cents on it.
>
> Miguel
>
> El jue, 26 nov 2026, 10:58, David Brown via Std-Proposals <std-
> proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> escribió:
>
> In C++26, we now have std::constant_wrapper<> and std::cw<> which can
> let you pass constant expressions as normal function parameters, and
> let
> them be used as constant expressions within the function definition.
> (We are talking about templated functions here, since each
> std::constant_wrapper<> instance has a unique type for the value it
> wraps.)
>
> From cppreference, we have the example :
>
> auto make_filled(auto size, int value)
> {
> std::array<int, size> a;
> a.fill(value);
> return a;
> }
>
> This can be called from other code as :
>
> auto a = make_filled(std::cw<3>, x);
>
> Then "a" is a std::array<3, int> filled with the value of "x".
>
> Prior to C++26, you would write this as :
>
> template<size_t size>
> auto make_filled(int value)
> {
> std::array<int, size> a;
> a.fill(value);
> return a;
> }
>
> auto a = make_filled<3>(x);
>
>
> The use of std::constant_wrapper<> can make the code a little neater
> and
> more consistent - the call site only uses function arguments and does
> not also need template arguments, and these can be given in the
> function
> definition in whatever order makes sense.
>
> But it means that every call needs to have "std::cw<>" wrappers
> explicitly added - that is more code at each call, more to add, more to
> get right, more to understand.
>
>
> My suggested alternative syntax here would be :
>
> auto make_filled(constexpr size_t size, int value)
> {
> std::array<int, size> a;
> a.fill(value);
> return a;
> }
>
> auto a = make_filled(3, x);
>
>
> Function parameters with the "constexpr" specifier would effectively be
> non-type template parameters. This would be another kind of
> abbreviated
> template, in the same sense as the use of concepts or "auto" in
> place of
> types for function parameters. A "constepxr" parameter could be of
> fixed type, "auto", a concept, etc., just like any other parameter.
>
> The advantage of this would be to give neater and more "universal"
> calling code for template functions with non-type template parameters.
> The disadvantage is that it hides the fact that the "size" parameter in
> this example must be a constant expression, not a runtime value.
>
>
> Any thoughts here? Is it an idea that appeals to people, or would it
> feel like it is hiding important information? Or am I missing
> something
> crucial that would make this impossible or impractical?
>
> David
>
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> <https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals>
>
> What would be the difference with a normal template parameter other than
> the callsite?
>
It would be different at the call site - the call would look more like a
normal function. And it would be different at the definition site,
giving a more abbreviated template structure to the code.
But it would not allow anything that you could not do from before - it
would just make things a little neater (IMHO).
> I am not too familiar with std::cw, but it seems to solve a common issue
> for me: a function can be implemented with a compile time size or a
> runtime size, but compile time information is preferable if available.
> Such a function could accept 'auto size', check if this value is a
> constant literal, and specialize accordingly. In such scenario the
> caller can provide runtime or compile time information to the callee
> without knowing it itself wether this information is usable at compile time.
>
Sure. The suggestion would not negate all use-cases for std::cw. In
particular, a function that has a constexpr parameter could only be
called with a constant expression argument - just like a non-type
template argument. The use of std::cw<> can let you have functions that
can take either constant expressions or value expressions, with a
definition that can have "if constexpr" checks to distinguish them. If
you wanted to achieve that with constexpr parameters, you'd have to have
two functions - one with a constexpr parameter, the other with no
"constexpr" on the parameter. (Such overloading resolution would be a
complication in making this feature, but I think it should be resolvable.)
David
> Just my two cents on it.
>
> Miguel
>
> El jue, 26 nov 2026, 10:58, David Brown via Std-Proposals <std-
> proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> escribió:
>
> In C++26, we now have std::constant_wrapper<> and std::cw<> which can
> let you pass constant expressions as normal function parameters, and
> let
> them be used as constant expressions within the function definition.
> (We are talking about templated functions here, since each
> std::constant_wrapper<> instance has a unique type for the value it
> wraps.)
>
> From cppreference, we have the example :
>
> auto make_filled(auto size, int value)
> {
> std::array<int, size> a;
> a.fill(value);
> return a;
> }
>
> This can be called from other code as :
>
> auto a = make_filled(std::cw<3>, x);
>
> Then "a" is a std::array<3, int> filled with the value of "x".
>
> Prior to C++26, you would write this as :
>
> template<size_t size>
> auto make_filled(int value)
> {
> std::array<int, size> a;
> a.fill(value);
> return a;
> }
>
> auto a = make_filled<3>(x);
>
>
> The use of std::constant_wrapper<> can make the code a little neater
> and
> more consistent - the call site only uses function arguments and does
> not also need template arguments, and these can be given in the
> function
> definition in whatever order makes sense.
>
> But it means that every call needs to have "std::cw<>" wrappers
> explicitly added - that is more code at each call, more to add, more to
> get right, more to understand.
>
>
> My suggested alternative syntax here would be :
>
> auto make_filled(constexpr size_t size, int value)
> {
> std::array<int, size> a;
> a.fill(value);
> return a;
> }
>
> auto a = make_filled(3, x);
>
>
> Function parameters with the "constexpr" specifier would effectively be
> non-type template parameters. This would be another kind of
> abbreviated
> template, in the same sense as the use of concepts or "auto" in
> place of
> types for function parameters. A "constepxr" parameter could be of
> fixed type, "auto", a concept, etc., just like any other parameter.
>
> The advantage of this would be to give neater and more "universal"
> calling code for template functions with non-type template parameters.
> The disadvantage is that it hides the fact that the "size" parameter in
> this example must be a constant expression, not a runtime value.
>
>
> Any thoughts here? Is it an idea that appeals to people, or would it
> feel like it is hiding important information? Or am I missing
> something
> crucial that would make this impossible or impractical?
>
> David
>
>
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> <https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals>
>
Received on 2026-11-26 11:25:23
