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, Sabina

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:

Advertisement

6 responses to “C++23: -> and :: to be replaced by . operator”

  1. IDontBotherAnymore Avatar
    IDontBotherAnymore

    Book title is “Fundamental algorithm in Java”

    Like

  2. Dammit. You had me up until garbage collection.

    Like

  3. ouch ! I felt scared up to your comment on the posting date. I feel better now 🙂

    Like

  4. […] this is not a trolling post to make a fool out of you. This year’s April Fool post is here, in case you missed the once-in-a-lifetime C++ joke of its kind. This article is about slowing the […]

    Like

  5. […] may like 2019 April Fool post: C++23: fullptr to replace nullptr and 2021 April’s Fool post: C++23: -> and :: to be replaced by . operator. […]

    Like

  6. […] out 2020 April’s Fool post: C++23: Mutable string_view and 2021 April’s Fool post: C++23: -> and :: to be replaced by . operator. […]

    Like

Leave a 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 )

Facebook photo

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

Connecting to %s

%d bloggers like this: