C++ Logo

std-discussion

Advanced search

Re: constexpr functions and variables

From: Tiago Freire <tmiguelf_at_[hidden]>
Date: Wed, 3 Apr 2024 14:09:05 +0000
> You might have also misinterpreted my mail.
> I'm not asking if it is possible to do it with variables, I already know it is possible.
> The question is if something similar was considered for functions, because there are similar advantages.

Sorry if I have misunderstood your email.

That it works for variables is already a stretch of the language.
And this only works if the .cpp file that declares
constexpr int myconstant;
sees
extern const int myconstant;

otherwise, it doesn't work.
It can't for the exact same reasons. The compiler wouldn't know.
But in the function case there's a far more elegant solution that satisfies what you want to do.

Have you tried

int function() { return function_constexpr(); }
 ?

Because you can just do that, and has exactly the same properties, except for preserving the name.
But the point still stands, cpp modules will fix this.
You could make a proposal to make this work in the interim.

And I don't think there's going to be an appetite for this given that:
a) modules will fix it
b) there's a trivial work around
c) right now compilers don't have to emit code if they are not used, and they would have too as a consequence
d) the use of it would be quite limited to justify the work

The idea is sound, not entirely sure if anyone else has proposed this before (someone else can comment on that).
But I wouldn't be surprised if it hasn't because of the above.

Received on 2024-04-03 14:09:10