2010-11-24 2 views
6
namespace M{ 
    void f(); 
    void M::f(){} 
} 

int main(){} 

Приведенный выше код дает ошибку следующим образом:пространство имен определение член

"ComeauTest.c", line 3: error: qualified name is not allowed in namespace member declaration void M::f(){}

И

G++ also gives error.

Но

VS2010 compiles fine.

Мои вопросы:

a) Какое ожидаемое поведение?

b) $ 7.3.1.2, похоже, не говорит об этом ограничении. Какая часть Стандарта определяет поведение такого кода?

+0

Проверьте мой ответ. :) – 2010-12-01 04:48:28

ответ

5

Which portion of the Standard guides the behavior of such code?

C++ 03 Раздел $ 8,3 говорит

A declarator-id shall not be qualified except for the definition of a member function (9.3) or static data member (9.4) out-side of its class, the definition or explicit instantiation of a function or variable member of a namespace out-side of its namespace, or the definition of a previously declared explicit specialization outside of its name-space, or the declaration of a friend function that is a member of another class or namespace (11.4).

Так что ваш код плохо сформирован.

Однако, обсуждая issue 548, CWG согласилась с тем, что запрет квалифицированных деклараторов внутри их пространства имен должен быть снят .

1: Активный Выпуск 482

0

7.3.1.2-2 переговоры специально по этому поводу:

Members of a named namespace can also be defined outside that namespace by explicit qualification (3.4.3.2) of the name being defined, provided that the entity being defined was already declared in the namespace and the definition appears after the point of declaration in a namespace that encloses the declaration’s namespace.

M::f считается за пределами определения пространства имен.

Смежные вопросы