C++ Logo

std-proposals

Advanced search

Re: [std-proposals] constexpr specifier for function parameters

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 26 Nov 2026 14:47:16 +0100
See here and the Kona meeting   https://github.com/cplusplus/papers/issues/603    -----Ursprüngliche Nachricht----- Von:David Brown via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Do 26.11.2026 13:14 Betreff:Re: [std-proposals] constexpr specifier for function parameters An:std-proposals_at_[hidden]; CC:David Brown <david.brown_at_[hidden]>; It looks like P1045 covers pretty much everything I have thought about here, and in much greater detail.  It seems to have achieved initial strong interest, then stalled.  Is that a sign that the idea is dead, or just that the original author was unwilling or unable to take the process further? David On 26/11/2026 12:15, Ivan Lazaric via Std-Proposals wrote: > IMO "other than the callsite" is not minor, it trivializes "generic over > is-constant-expression?" > Or, depends how you understand details of what is proposed, > I operate under "constexpr fn parameter can be either constant > expression lifted to template parameter, > or regular old runtime thing", + consteval fn parameters that reject latter. > > ```cpp > void foo(float* ptr, constexpr size_t len) { ... } > > std::array<float, 5> arr = ...; > foo(arr.data(), arr.size()); // foo<5>(float*) > std::vector<float> vec = ...; > foo(vec.data(), vec.size()); // foo(float*, size_t) > ``` > > Relevant proposal: P1045 - constexpr Function Parameters > https://web.archive.org/web/20261110020406/https://www.open-std.org/ > jtc1/sc22/wg21/docs/papers/2019/p1045r1.html <https://web.archive.org/ > web/20261110020406/https://www.open-std.org/jtc1/sc22/wg21/docs/ > papers/2019/p1045r1.html> > https://github.com/cplusplus/papers/issues/603 <https://github.com/ > cplusplus/papers/issues/603> > > On Thu, Nov 26, 2026 at 11:35 AM Miguel Veganzones via Std-Proposals > <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>> > wrote: > >     What would be the difference with a normal template parameter other >     than the callsite? > >     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. > >     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> > >     -- >     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> > > -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-11-26 13:49:10