Skip to content

Commit d717a09

Browse files
committed
Безымянные пространства имен внутри именованных.
1 parent ce3d18d commit d717a09

File tree

15 files changed

+469
-0
lines changed

15 files changed

+469
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- [Глобальные переменные в глобальном пространстве имён](syncfusion/cpp_succinctly/GlobalNamespaceSample/)
44
- [Определение вложенных пространств имён](syncfusion/cpp_succinctly/NamespacesSample/)
5+
- [Безымянные пространства имен внутри именованных](ns/unnamed-namespace-within-named)
56

67

78
# Переменные

ns/unnamed-namespace-within-named/.gitignore

+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# ---------------------------------------------------------------------------
2+
# https://github.com/github/gitignore/blob/master/C++.gitignore
3+
4+
# Prerequisites
5+
*.d
6+
7+
# Compiled Object files
8+
*.slo
9+
*.lo
10+
*.o
11+
*.obj
12+
13+
# Precompiled Headers
14+
*.gch
15+
*.pch
16+
17+
# Compiled Dynamic libraries
18+
*.so
19+
*.dylib
20+
*.dll
21+
22+
# Fortran module files
23+
*.mod
24+
*.smod
25+
26+
# Compiled Static libraries
27+
*.lai
28+
*.la
29+
*.a
30+
*.lib
31+
32+
# Executables
33+
*.exe
34+
*.out
35+
*.app
36+
37+
# ---------------------------------------------------------------------------
38+
# https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore
39+
40+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
41+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
42+
43+
# User-specific stuff
44+
.idea/**/workspace.xml
45+
.idea/**/tasks.xml
46+
.idea/**/usage.statistics.xml
47+
.idea/**/dictionaries
48+
.idea/**/shelf
49+
50+
# AWS User-specific
51+
.idea/**/aws.xml
52+
53+
# Generated files
54+
.idea/**/contentModel.xml
55+
56+
# Sensitive or high-churn files
57+
.idea/**/dataSources/
58+
.idea/**/dataSources.ids
59+
.idea/**/dataSources.local.xml
60+
.idea/**/sqlDataSources.xml
61+
.idea/**/dynamic.xml
62+
.idea/**/uiDesigner.xml
63+
.idea/**/dbnavigator.xml
64+
65+
# Gradle
66+
.idea/**/gradle.xml
67+
.idea/**/libraries
68+
69+
# Gradle and Maven with auto-import
70+
# When using Gradle or Maven with auto-import, you should exclude module files,
71+
# since they will be recreated, and may cause churn. Uncomment if using
72+
# auto-import.
73+
# .idea/artifacts
74+
# .idea/compiler.xml
75+
# .idea/jarRepositories.xml
76+
# .idea/modules.xml
77+
# .idea/*.iml
78+
# .idea/modules
79+
# *.iml
80+
# *.ipr
81+
82+
# CMake
83+
cmake-build-*/
84+
85+
# Mongo Explorer plugin
86+
.idea/**/mongoSettings.xml
87+
88+
# File-based project format
89+
*.iws
90+
91+
# IntelliJ
92+
out/
93+
94+
# mpeltonen/sbt-idea plugin
95+
.idea_modules/
96+
97+
# JIRA plugin
98+
atlassian-ide-plugin.xml
99+
100+
# Cursive Clojure plugin
101+
.idea/replstate.xml
102+
103+
# SonarLint plugin
104+
.idea/sonarlint/
105+
106+
# Crashlytics plugin (for Android Studio and IntelliJ)
107+
com_crashlytics_export_strings.xml
108+
crashlytics.properties
109+
crashlytics-build.properties
110+
fabric.properties
111+
112+
# Editor-based Rest Client
113+
.idea/httpRequests
114+
115+
# Android studio 3.1+ serialized cache file
116+
.idea/caches/build_file_checksums.ser
117+
118+
# ---------------------------------------------------------------------------
119+
# https://github.com/github/gitignore/blob/master/Global/macOS.gitignore
120+
121+
# General
122+
.DS_Store
123+
.AppleDouble
124+
.LSOverride
125+
126+
# Icon must end with two \r
127+
Icon
128+
129+
# Thumbnails
130+
._*
131+
132+
# Files that might appear in the root of a volume
133+
.DocumentRevisions-V100
134+
.fseventsd
135+
.Spotlight-V100
136+
.TemporaryItems
137+
.Trashes
138+
.VolumeIcon.icns
139+
.com.apple.timemachine.donotpresent
140+
141+
# Directories potentially created on remote AFP share
142+
.AppleDB
143+
.AppleDesktop
144+
Network Trash Folder
145+
Temporary Items
146+
.apdisk
147+
148+
# ---------------------------------------------------------------------------
149+
# https://github.com/github/gitignore/blob/master/CMake.gitignore
150+
151+
CMakeLists.txt.user
152+
CMakeCache.txt
153+
CMakeFiles
154+
CMakeScripts
155+
Testing
156+
Makefile
157+
cmake_install.cmake
158+
install_manifest.txt
159+
compile_commands.json
160+
CTestTestfile.cmake
161+
_deps
162+
163+
# ---------------------------------------------------------------------------

ns/unnamed-namespace-within-named/.idea/.gitignore

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ns/unnamed-namespace-within-named/.idea/.name

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ns/unnamed-namespace-within-named/.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ns/unnamed-namespace-within-named/.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ns/unnamed-namespace-within-named/.idea/unnamed-namespace-within-named.iml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ns/unnamed-namespace-within-named/.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// Created by Abbas Gussenov on 2/23/22.
3+
//
4+
5+
#include "A.h"
6+
#include <iostream>
7+
8+
// void Foo() { std::cout << "Foo() in A.cc" << std::endl; }
9+
// duplicate symbol 'Foo()' in:
10+
// CMakeFiles/UnnamedNamespaceWithinNamedNamespace.dir/A.cc.o
11+
// CMakeFiles/UnnamedNamespaceWithinNamedNamespace.dir/B.cc.o
12+
// ld: 1 duplicate symbol for architecture x86_64
13+
14+
// Объявление, чтобы можно было вызывать:
15+
namespace {
16+
namespace {
17+
namespace {
18+
void Quuux();
19+
}
20+
}
21+
}
22+
23+
void Bat()
24+
{
25+
// Bar();
26+
// Undefined symbols for architecture x86_64:
27+
// "Bar()", referenced from:
28+
// Bat() in A.cc.o
29+
// Baz() in B.cc.o
30+
// ld: symbol(s) not found for architecture x86_64
31+
32+
// Qux(); // Use of undeclared identifier 'Qux'
33+
Quuux();
34+
}
35+
36+
namespace
37+
{ // An unnamed namespace hide names inside it from different translation units.
38+
39+
void Bar() { std::cout << "Bar() in A.cc" << std::endl; }
40+
41+
void Qux() { std::cout << "Qux() in B.cc" << std::endl; }
42+
namespace
43+
{
44+
void Quux() { std::cout << "Quux() in A.cc" << std::endl; }
45+
namespace
46+
{
47+
void Quuux() { std::cout << "Quuux() in A.cc" << std::endl; }
48+
}
49+
}
50+
51+
}
52+
53+
void Temp()
54+
{
55+
// Bar(); // Call to 'Bar' is ambiguous
56+
57+
Quuux();
58+
}
59+
60+
void Sub()
61+
{
62+
// qwe::Asd();
63+
// Undefined symbols for architecture x86_64:
64+
// "qwe::Asd()", referenced from:
65+
// Sub() in A.cc.o
66+
// ld: symbol(s) not found for architecture x86_64
67+
//
68+
// Объявлено как qwe::Asd(), а определено как qwe::(anonymous namespace)::Asd(),
69+
// поэтому не находит.
70+
71+
qwe::Zxc();
72+
// Объявление qwe::(anonymous namespace)::Zxc() и определение совпадают поэтому ОК.
73+
}
74+
75+
namespace qwe
76+
{
77+
78+
// void DoTheStuff() { std::cout << "DoTheStuff() in A.cc" << std::endl; }
79+
// duplicate symbol 'qwe::DoTheStuff()' in:
80+
// CMakeFiles/UnnamedNamespaceWithinNamedNamespace.dir/A.cc.o
81+
// CMakeFiles/UnnamedNamespaceWithinNamedNamespace.dir/B.cc.o
82+
// ld: 1 duplicate symbol for architecture x86_64
83+
84+
namespace
85+
{ // An unnamed namespace hide names inside it from different translation units.
86+
87+
void Asd() { std::cout << "Asd() in A.cc" << std::endl; }
88+
void Zxc() { std::cout << "Zxc() in A.cc" << std::endl; }
89+
void Aaa() { std::cout << "Aaa() in A.cc" << std::endl; }
90+
91+
}
92+
93+
}

ns/unnamed-namespace-within-named/A.h

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Created by Abbas Gussenov on 2/23/22.
3+
//
4+
5+
#ifndef UNNAMED_NAMESPACE_WITHIN_NAMED_A_H
6+
#define UNNAMED_NAMESPACE_WITHIN_NAMED_A_H
7+
8+
9+
void Bat();
10+
11+
void Bar();
12+
13+
namespace qwe
14+
{
15+
16+
void Asd();
17+
18+
namespace
19+
{
20+
void Zxc();
21+
void Aaa(); // function 'qwe::(anonymous namespace)::Aaa' has internal linkage
22+
}
23+
24+
}
25+
26+
void Sub();
27+
28+
#endif //UNNAMED_NAMESPACE_WITHIN_NAMED_A_H
+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//
2+
// Created by Abbas Gussenov on 2/23/22.
3+
//
4+
5+
#include "B.h"
6+
#include <iostream>
7+
8+
// void Foo() { std::cout << "Foo() in B.cc" << std::endl; }
9+
// duplicate symbol 'Foo()' in:
10+
// CMakeFiles/UnnamedNamespaceWithinNamedNamespace.dir/A.cc.o
11+
// CMakeFiles/UnnamedNamespaceWithinNamedNamespace.dir/B.cc.o
12+
// ld: 1 duplicate symbol for architecture x86_64
13+
14+
void Baz()
15+
{
16+
// Bar();
17+
// Undefined symbols for architecture x86_64:
18+
// "Bar()", referenced from:
19+
// Bat() in A.cc.o
20+
// Baz() in B.cc.o
21+
// ld: symbol(s) not found for architecture x86_64
22+
}
23+
24+
namespace
25+
{ // An unnamed namespace hide names inside it from different translation units.
26+
27+
void Bar() { std::cout << "Bar() in B.cc" << std::endl; }
28+
29+
void Qux() { std::cout << "Qux() in B.cc" << std::endl; }
30+
namespace
31+
{
32+
void Quux() { std::cout << "Quux() in B.cc" << std::endl; }
33+
namespace
34+
{
35+
void Quuux() { std::cout << "Quuux() in B.cc" << std::endl; }
36+
}
37+
}
38+
39+
}
40+
41+
void Test()
42+
{
43+
// Bar(); // Call to 'Bar' is ambiguous
44+
45+
Quuux();
46+
}
47+
48+
namespace qwe
49+
{
50+
51+
// void DoTheStuff() { std::cout << "DoTheStuff() in B.cc" << std::endl; }
52+
// duplicate symbol 'qwe::DoTheStuff()' in:
53+
// CMakeFiles/UnnamedNamespaceWithinNamedNamespace.dir/A.cc.o
54+
// CMakeFiles/UnnamedNamespaceWithinNamedNamespace.dir/B.cc.o
55+
// ld: 1 duplicate symbol for architecture x86_64
56+
57+
namespace
58+
{ // An unnamed namespace hide names inside it from different translation units.
59+
60+
void Asd() { std::cout << "Asd() in B.cc" << std::endl; }
61+
void Zxc() { std::cout << "Zxc() in B.cc" << std::endl; }
62+
63+
}
64+
65+
}

0 commit comments

Comments
 (0)