C++ Logo

std-proposals

Advanced search

[std-proposals] Allow static conversion function

From: Kilian Henneberger <kilis-mail_at_[hidden]>
Date: Sun, 20 Nov 2022 14:51:47 +0000
Hello,

following two proposal got accepted for C++23:
static operator() P1169R4 <https://wg21.link/P1169R4>
static operator[] P2589R0 <https://wg21.link/P2589R0>
Thanks a lot to the authors and everyone involved!

I want to raise the idea of also allowing conversion functions to be static.

template<auto X>
struct ConvertsTo {
     static operator auto() { return X; }
};

I don't know, if code like this actually exists today and would benefit
from my proposed change.

Lambdas:
Conversion functions which would benefit from being static, are the ones
of stateless lambdas:

auto L = [](int a){ return a * a; };
int(*Fptr)(int) = L;

P1169R4#lambdas
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1169r4.html#lambdas>
explains, that making the callable operator of a stateless lambda
implicitly static, is an API and ABI break. Therefore, you have to
explicitly request the callable operator to be static: [](int a) static
{ return a * a; };.
In such situations, we could make the conversion operator of a lambda
static, too.

The proposed wording (in my opinion) would be, to remove the adjective
"non-static" from https://eel.is/c++draft/class.conv.fct#2 and regarding
the lambda the wording of
https://eel.is/c++draft/expr.prim.lambda.closure#8 would need some small
adjustment.

In 2001, Scott Meyers opened
https://cplusplus.github.io/CWG/issues/296.html and asked about this
exact topic. The resolution at this time has been to explicitly prohibit
static conversion functions. What is our take on that today?

Kind regards,
Kilian

Received on 2022-11-20 14:51:51