C++23: -> and :: to be replaced by . operator

In the biggest change to C++ syntax, -> and :: operators are to be replaced by . operator in the upcoming C++23 AF00L proposal. The rationale behind this proposal is there can be no confusion (as to which operator to use).

The :: operator is not only used in scope resolution and also as class-to-static_member. However, another way to refer to a static member is through the object instance. All these leads to subtle confusion as to what :: actually denotes.

// class with static member
struct Widget
{
    static int Count;
};

Widget o;
Widget* p = &o;

// Accessing Widget's static Count
int a = Widget::Count;
int b = o.Count;
int c = p->Count;

The :: operator is also used by scoped enum to refer to its enumerators.

// enum class
enum class Fruit
{
    Apple,
    Orange,
    Banana
};

// usage of scoped enum
Fruit::Orange

Efforts to mimick the C++11 scoped enum syntax without its safety is by enclosing unscoped enum inside a namespace or class. So :: operator actually does not tell the developer reading the enum usage about the enum type.

// namespace scoped enum 
namespace Fruit
{
    enum fruit
    {
        Apple,
        Orange,
        Banana
    }
};

// usage of namespace scoped enum
Fruit::Orange

Moreover, when an unscoped enum is a class member, its enumerators may be accessed using class member access operators . and ->. All these adds up to the confusion.

// class scoped enum 
struct Fruit
{
    enum fruit
    {
        Apple,
        Orange,
        Banana
    }
};

// usage of class scoped enum
Fruit::Orange

Fruit x;
Fruit* p = &x;

int c = x.Orange;
int d = p->Orange;

Similarly, the -> operator is not only used in pointer-to-member but also to indicate return type of lambda.

// Example of C++11 lambda with boolean return type
auto lambda = []() -> bool {...}

In C++17, the -> operator is used in type deduction guides(See code example below).

// C++17 Deduction Guides 
template<typename T> 
struct S 
{
    T val;
};

// map string literals to S<std::string>
S(const char*) -> S<std::string>;

In an effort to end all confusion in one fell swoop, -> and :: operators are going to be deprecated in favor of the universal . operator.

In addition to this new development, C++23 is going to get garbage collection to manage its memory and every C++ class inherits automatically from the base Object class!

russian_student

“That’s great news! I can make use of my father’s 1995 original Java textbook to learn C++23!” – Russian college student, Annika

Before anyone makes a serious commentary to this blog post, take a good look at the posting date!

For those who like this year’s April’s Fool post, be sure to check out 2019 and 2020 April’s Fool post:

4 thoughts on “C++23: -> and :: to be replaced by . operator

Leave a Reply to IDontBotherAnymore Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: