C++ Logo


Advanced search

[std-proposals] Allow chaining of -> for normal pointers

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sat, 7 Jan 2023 18:52:31 +0000
Chaining of 'operator->' is allowed for user-defined classes, for example:

#include <string> // string

class B;

struct A {
    B &operator->(void);

class C;

struct B {
    C &operator->(void);

struct C {
    std::string *operator->(void) { static std::string s; return &s; }

B &A::operator->(void) { static B b; return b; }
C &B::operator->(void) { static C c; return c; }

#include <iostream>

int main(void)
    A obj;

    obj->insert(0u, "World");
    obj->insert(0u, "Hello ");

    std::cout << obj->c_str() << std::endl;

I was thinking it would be convenient if this were also allowed with
normal pointers, for example:

    1: string s;
    2: string *p = &s;
    3: string **pp = &p;
    4: string ***ppp = &pp;
    5: string ****pppp = &ppp;
    7: (***pppp)->insert(0u, "hello world");

For example we could replace Line #7 with:

    pppp->insert(0u, "hello world");

When I first started wondering about the implications of this core
language change, I first thought that it might break old code that
relies on SFINAE, but I tried writing a template function with
"std::enable_if" and I don't actually think that this change to the
core language would break any SFINAE achieved with "std::enable_if".

With C++20/23 though, this core language change could make a
'requires' clause true which was previously false.

Was there every any discussion about allowing "->" chaining for pointers?

Received on 2023-01-07 18:52:42