2

Would be handy and nice to write something like this:

void f(double a, int constexpr b, bool c) {}

i.e. to mix non-constexpr and constexpr params inside a regular (runtime, non constexpr) function. Also call of such function would be clean and simple as f(a, 123, b) without difficult to read template params.

Of cause function above should behave (and implemented in compiler underneath) as if it was just a regular templated function (with different order of params):

template <int b>
void f(double a, bool c) {}

And this template variant above can be used instead of first variant, but first variant is still very nice as a syntax sugar of the second variant.

For example latest C++ already has a nice syntax sugar for having both params and return value as auto (same like in lambdas):

auto f(int x, auto y) { return y; }

Sure one may ask why not to have all params as runtime params, like in function below, anyway compilers often inline everything:

void f(double a, int b, bool c) {}

But this runtime-passing of all params not always guarantees inlining and/or other compile-time-constant optimizations. Also it doesn't allow to use variable b within constexpr context for example inside if constexpr().

My question is why C++ still has no such feature as mix of constexpr and non-constexpr params among function's params? Also maybe there are proposals or future plans for that?

Arty
  • 14,883
  • 6
  • 36
  • 69

1 Answers1

2

There seems to be some standardization effort, but I would say duplicate functionality is hardly useful. Just pass the constexpr arguments as template arguments. How would your version interact with function pointers? Are you also proposing int constexpr should be a distinct type? Or are you proposing it should be nicer-syntax for the templated version? Should this compile:

void foo(int constexpr);

void (*fp)(int) = foo;

If you can nicely fit this idea into the C++ type system, and you feel the gain is worth the standardization effort, have a look at How To Submit a Proposal.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • Seems the advantage might be that `constexpr` function is just regular a function; it could be evaluated either at compile-time or run-time. But then, all you've really done is double work, since the optimizer is already going to do constant folding whenever possible. – Cody Gray - on strike Jun 11 '21 at 08:51
  • I want just a nicer syntax sugar for templated function. Same like already starting from C++20 you can write a regular function with `auto` both in params and in return type e.g. `auto f(int x, auto y) { return y; }`. As you can see this `auto`-syntax is a duplicate of templated function, but still it was included in standard right now in C++20. – Arty Jun 11 '21 at 08:51
  • 3
    [P1045: `constexpr` Function Parameters](https://wg21.link/p1045) – Evg Jun 11 '21 at 08:52
  • @Evg Yes, exactly like this proposal I was waiting for. Do you know if it has any success to become a future standard? – Arty Jun 11 '21 at 08:53
  • @Arty No. (dummy text) – Evg Jun 11 '21 at 08:53
  • @Evg thank you for the link. I added that to my answer. – Aykhan Hagverdili Jun 11 '21 at 08:53
  • Ayxan Haverdili The code you provided should be as compilable as the code for a function `template void foo();`. Basically I was just proposing a nicer replacement for `template ` function. And underneath compiler should definitely just replace my proposed style with regular `template`. – Arty Jun 11 '21 at 08:55
  • 1
    I think duplicate functionality is very useful as far as it makes writing code easier, nicer and more readable. In all popular languages there are different syntax sugar constructs that express one paradigm with many different ways to write it. And I think calling a function like `f(a, 123, b)` is much nicer and readable than `f<123>(a, b)`. – Arty Jun 11 '21 at 08:58
  • @Arty your proposal in theory is not bad. I am not saying "it's a bad idea". I am just saying there is more work to do to get it into the language. Volunteer work is limited and I'd rather have the committee work on more serious problems than more syntax sugar. – Aykhan Hagverdili Jun 11 '21 at 08:59
  • Do you know where I can see statuses of proposals? I want to find out [about this one](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1045r1.html) if it was rejected or something else happened to it? – Arty Jun 11 '21 at 09:03
  • @Arty If you happen to find anything about the status, please update your question. – Evg Jun 11 '21 at 09:05
  • 2
    @Arty googling `P1045` brings up https://github.com/cplusplus/papers/issues/603 – Aykhan Hagverdili Jun 11 '21 at 09:07
  • 1
    @Arty I don't know about this specific proposal, but I think the feature will be in C++ by 2030. – JDługosz Jun 11 '21 at 13:46