514. Is the initializer for a namespace member in the scope
От: Lorenzo_LAMAS  
Дата: 09.12.05 07:51
Оценка:
Увидел тут

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html

514. Is the initializer for a namespace member in the scope of the namespace?
Section: 3.4.1 basic.lookup.unqual Status: review Submitter: Mike Miller Date: 24 Mar 2005

Is the following code well-formed?

namespace N {
int i;
extern int j;
}
int N::j = i;

The question here is whether the lookup for i in the initializer of N::j finds the declaration in namespace N or not. Implementations differ on this question.

If N::j were a static data member of a class, the answer would be clear: both 3.4.1 basic.lookup.unqual paragraph 12 and 8.5 dcl.init paragraph 11 say that the initializer “is in the scope of the member's class.” There is no such provision for namespace members defined outside the namespace, however.

The reasoning given in 3.4.1 basic.lookup.unqual may be instructive:

A name used in the definition of a static data member of class X (9.4.2 class.static.data) (after the qualified-id of the static member) is looked up as if the name was used in a member function of X.
It is certainly the case that a name used in a function that is a member of a namespace is looked up in that namespace (3.4.1 basic.lookup.unqual paragraph 6), regardless of whether the definition is inside or outside that namespace. Initializers for namespace members should probably be looked up the same way.

Proposed resolution (October, 2005):

Add a new paragraph following 3.4.1 basic.lookup.unqual paragraph 12:

If a variable member of a namespace is defined outside of the scope of its namespace then any name used in the definition of the variable member (after the qualified-id) is looked up as if the definition of the variable member occurred in its namespace. [Example:

namespace N {
int i = 4;
extern int j;
}

int i = 2;

int N::j = i; // N::j == 4

—end example]

Я не понимаю, зачем добавлять новый параграф, ведь есть 3.4.3/3 (в свое время на него указал мне elcste, если я ничего не путаю по причине склероза). Или 3.4.3/3 не о том?
Of course, the code must be complete enough to compile and link.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.