Date: Thu, 28 May 2020 03:05:40 +0300
According to the C++ 20 Standard (6.5.1 Unqualified name lookup)
14 If a variable member of a namespace is defined outside of the scope of its namespace then any name that appears in the definition of the member (after the declarator-id) is looked up as if the definition of the member occurred in its namespace.
Now let’s consider the following program
#include <iostream>
namespace N1
{
namespace N2
{
int i = 10;
}
using namespace N2;
namespace N3
{
extern int x;
}
inline namespace N4
{
int j = 20;
}
}
int i = 1;
int j = 2;
int N1::N3::x = i + j;
int main()
{
std::cout << "N1::N3::x = " << N1::N3::x << '\n';
}
The MS Visual C++ 2019 produces the following result
N1::N3::x = 30
So several questions arise. The first one is the program valid? That is are the variable declarations
int i = 1;
int j = 2;
are visible in the this declaration
int N1::N3::x = i + j;
?
That is if the definition «occurs» in the namespace (according to the quote) then the names are not visible. If I am wrong then should the value of the variable x be equal to 12? Is it a bug of the compiler that outputs the value of x equal to 30?
With best regards,
(Vlad from Moscow)
You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or http://ru.stackoverflow.com
14 If a variable member of a namespace is defined outside of the scope of its namespace then any name that appears in the definition of the member (after the declarator-id) is looked up as if the definition of the member occurred in its namespace.
Now let’s consider the following program
#include <iostream>
namespace N1
{
namespace N2
{
int i = 10;
}
using namespace N2;
namespace N3
{
extern int x;
}
inline namespace N4
{
int j = 20;
}
}
int i = 1;
int j = 2;
int N1::N3::x = i + j;
int main()
{
std::cout << "N1::N3::x = " << N1::N3::x << '\n';
}
The MS Visual C++ 2019 produces the following result
N1::N3::x = 30
So several questions arise. The first one is the program valid? That is are the variable declarations
int i = 1;
int j = 2;
are visible in the this declaration
int N1::N3::x = i + j;
?
That is if the definition «occurs» in the namespace (according to the quote) then the names are not visible. If I am wrong then should the value of the variable x be equal to 12? Is it a bug of the compiler that outputs the value of x equal to 30?
With best regards,
(Vlad from Moscow)
You can meet me at http://cpp.forum24.ru/ or www.stackoverflow.com or http://ru.stackoverflow.com
Received on 2020-05-27 19:08:48