Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope resolution operator

Tags:

c++

scope

g++

I accidentally happened to find this in one of the source codes I was looking at. So, I'm giving a similar smaller example here.

In the file test.h:

#include<iostream>

class test{
    int i;
public:
    test(){}
    //More functions here
};

In the file test.cpp:

#include "test.h"

int main()
{
    test test1;
    test::test test2;
    test::test::test test3;
    return 0;
}

First of all, is there a reason to declare test2 that way? Secondly, this code compiles just fine in g++ version 4.4.3 and lower versions. Is there something in the C++ standard, saying, scope resolution operators are ignored when there is no need to resolve scope?

like image 980
Ashwin Avatar asked Apr 07 '12 01:04

Ashwin


People also ask

What is a scope resolution operator give an example?

The scope resolution operator ( :: ) is used for several reasons. For example: If the global variable name is same as local variable name, the scope resolution operator will be used to call the global variable. It is also used to define a function outside the class and used to access the static variables of class.

What is the :: operator in C++?

In C++, scope resolution operator is ::. It is used for following purposes. 2) To define a function outside a class. 3) To access a class's static variables.

Which of the following is the scope resolution operator?

Which of the following is the scope resolution operator? a) . Explanation: :: operator is called scope resolution operator used for accessing a global variable from a function which is having the same name as the variable declared in the function.

What is scope resolution operator in Java?

C++ supports the scope resolution operator (::) that allows us to resolve the ambiguous call or reference to identifiers. Like C++, Java does not support the scope resolution operator. Java uses the same operator (::) but with different names.


2 Answers

This code is not valid.

It was a bug in g++ that it accepted the code. See "g++ does not treat injected class name correctly." The bug was resolved as fixed in 2009, so it should be fixed in any recent version of g++.

like image 131
James McNellis Avatar answered Oct 14 '22 14:10

James McNellis


To clarify the situation, as specified in §9/2:

A class-name is inserted into the scope in which it is declared immediately after the class-name is seen. The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.

However, as specified in §3.4.3.1/1:

If the nested-name-specifier of a qualified-id nominates a class, the name specified after the nested-namespecifier is looked up in the scope of the class (10.2), except for the cases listed below.

[ ... §3.4.3.1/2]:

In a lookup in which the constructor is an acceptable lookup result and the nested-name-specifier nominates a class C:

— if the name specified after the nested-name-specifier, when looked up in C, is the injected-class-name of C (Clause 9) [ ... ] the name is instead considered to name the constructor of class C.

[ ... example: ]

struct A { A(); };
[ ... ]
A::A a; // error, A::A is not a type name
struct A::A a2; // object of type A
like image 23
Jerry Coffin Avatar answered Oct 14 '22 13:10

Jerry Coffin