19

It is rumoured that early C++ implementations have had dot operator for namespace access. There is an opinion that the dot is more convenient that the modern double colon operator.

What was the reasoning behind introducing the double colon?

7
  • 3
    Off the top of my head, C++ lets you do ::symbol to force it to look for symbol in the global namespace instead of the current namespace, so that's at least one thing which would look very confusing and probably introduce yet more syntactical ambiguity into the language if the dot operator was used instead.
    – Ixrec
    Apr 2, 2016 at 6:34
  • 18
    The::horribly::ugly proliferation::of the::C++ namespace::resolution::operator everywhere is::known::as colon::cancer. Apr 2, 2016 at 7:56
  • 1
    I guess it caused confusion in the semantic analyzer. Dot is also used for prefixing method names, which is a pretty similar concept and might be hard to isolate. This is a clear way to fix that. Apr 2, 2016 at 8:16
  • 2
    Some hypotheses in Why does C++ need the scope resolution operator?
    – manlio
    Apr 2, 2016 at 14:09
  • 5
    The horrible ugly proliferation of Mason Wheeler's senseless attacks on C has no name, because such evil must not be dignified with a name! Apr 2, 2016 at 23:00

1 Answer 1

15

As observed by Jules it's a fact that early C++ implementations (CFront pre-1.0) had a dot for scope identification.

A dot was also used in C with Classes (1980). Indeed this is a simple snippet from Classes: An Abstract Data Type Facility for the C Language 1:

class stack {
    char    s[SIZE];  /* array of characters */
    char *  min;      /* pointer to bottom of stack */
    char *  top;      /* pointer to top of stack */
    char *  max;      /* pointer to top of allocated space */
    void    new();    /* initialization function (constructor) */
public:
    void push(char);
    char pop();
};

char stack.pop()
{
    if (top <= min) error("stack underflow");
    return *(−−top);
}

(the code was an example of how member functions were typically defined "elsewhere")

The :: was one of the additions to C with Classes introduced to produce C++.

The reason is given by Stroustrup himself:

In C with Classes, a dot was used to express membership of a class as well as expressing selection of a member of a particular object.

This had been the cause of some minor confusion and could also be used to construct ambiguous examples. To alleviate this, :: was introduced to mean membership of class and . was retained exclusively for membership of object

(A History of C++: 1979−1991 [2] page 21 - § 3.3.1)


  1. Bjarne Stroustrup: "Classes: An Abstract Data Type Facility for the C Language" - Bell Laboratories Computer Science Technical Report CSTR−84. April 1980.

  2. Bjarne Stroustrup: "A History of C++: 1979−1991" - AT&T Bell Laboratories Murray Hill, New Jersey 07974.

1
  • 1
    Seems to me that . means membership would've been the simpler choice
    – mczarnek
    Jan 17, 2022 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.