C++ Logo

std-discussion

Advanced search

Re: constexpr functions and variables

From: Barry Revzin <barry.revzin_at_[hidden]>
Date: Wed, 3 Apr 2024 09:34:29 -0500
On Wed, Apr 3, 2024, 9:09 AM Tiago Freire via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> > 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.
>


Yes, the original post is very clear that he tried that. He explicitly
listed the shortcomings of that approach.


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
>


While I agree with (d), I'm not sure what about modules "fix" this? You
could still, in a module, want to export the signature and in an
implementation unit it define it as constexpr and use it as such in that
specific translation unit.


> 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.
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2024-04-03 14:34:48