+ 9

In C++ STL, does every name exist only once?

In Python's stdlib there would be name crashes if you imported every name at once into your code. For example the name 'time' occurs in the modules datetime and time, meaning different things. In C++, starting with std:: you can access all these names. Is it taken care of that no name occurs twice in that namespace?

8th May 2019, 3:46 PM
HonFu
HonFu - avatar
2 ответов
+ 7
As far as I know, all the names are unique. Two classes or variables cannot have the same name inside the same scope, and that is simply what a namespace defines; a new scope. If a custom library wishes to provide an alternate implementation of std::vector or another class in the std namespace, they can do so by specifying their implementation inside a new namespace. The one required for use can be simply accessed by specifying the scope. This is why using namespace statements lead to name collisions, like in the case where you use the filesystem library and have the statements 'using namespace std' and 'using namespace boost' in your code. The using namespace statement makes the underlying classes, variables and functions visible in the scope in which the using statement was used as if they were declared in this scope containing the using directive. This is why these statements can lead to ambiguity and its best to access using explicit qualification, by specifying their complete scope unless implied.
8th May 2019, 5:49 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 6
They most probably designed it with unique names but I am only guessing.
9th May 2019, 6:42 AM
Sonic
Sonic - avatar