├── inline_namespace ├── il_03.cpp ├── il_05_2.cpp ├── il_05_1.cpp ├── il_01.cpp ├── il_04.cpp └── il_02.cpp ├── using_declaration ├── ud_02.cpp ├── ud_01.cpp ├── ud_04.cpp ├── ud_05.cpp ├── ud_06.cpp └── ud_03.cpp ├── unnamed_namespace ├── un_02.cpp ├── un_03.cpp ├── un_01.cpp └── un_04.cpp ├── adl ├── adl_11.cpp ├── adl_16.cpp ├── adl_15.cpp ├── adl_09.cpp ├── adl_05.cpp ├── adl_14.cpp ├── adl_06.cpp ├── adl_12.cpp ├── adl_04.cpp ├── adl_01.cpp ├── adl_13.cpp ├── adl_10.cpp ├── adl_03.cpp ├── adl_07.cpp └── adl_02.cpp ├── nested_namespace ├── nested_01.cpp ├── nested_07.cpp ├── nested_10.cpp ├── nested_11.cpp ├── nested_03.cpp ├── nested_05.cpp ├── nested_08.cpp ├── nested_06.cpp ├── nested_02.cpp ├── nested_04.cpp └── nested_09.cpp ├── using_directive ├── using_directive_03.cpp ├── using_directive_13.cpp ├── using_directive_05.cpp ├── using_directive_10.cpp ├── using_directive_11.cpp ├── using_directive_09.cpp ├── using_directive_04.cpp ├── using_directive_07.cpp ├── using_directive_06.cpp ├── using_directive_12.cpp ├── using_directive_01.cpp ├── using_directive_02.cpp └── using_directive_08.cpp ├── basic ├── basic_03.cpp ├── basic_06.cpp ├── basic_02.cpp ├── basic_01.cpp ├── basic_04.cpp └── basic_05.cpp ├── namespace_alias ├── na_01.cpp ├── na_03.cpp ├── na_02.cpp ├── na_05.cpp └── na_04.cpp └── overloading ├── overloading_03.cpp ├── overloading_04.cpp ├── overloading_02.cpp ├── overloading_06.cpp ├── overloading_01.cpp ├── overloading_07.cpp └── overloading_05.cpp /inline_namespace/il_03.cpp: -------------------------------------------------------------------------------- 1 | namespace A::B:: inline C { //C++20 2 | int x; 3 | } 4 | 5 | int main() 6 | { 7 | A::B::x = 5; 8 | } 9 | -------------------------------------------------------------------------------- /using_declaration/ud_02.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | int x, y, z; 3 | } 4 | 5 | int main() 6 | { 7 | using nec::x; 8 | x = 10; 9 | int x = 20; //invalid 10 | } 11 | -------------------------------------------------------------------------------- /using_declaration/ud_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | using std::string, std::cout; 7 | 8 | cout << string{ "necati" }; 9 | } 10 | -------------------------------------------------------------------------------- /unnamed_namespace/un_02.cpp: -------------------------------------------------------------------------------- 1 | namespace nec 2 | { 3 | namespace 4 | { 5 | int x; 6 | //... 7 | } 8 | } 9 | 10 | 11 | int main() 12 | { 13 | nec::x = 5; // valid 14 | } 15 | -------------------------------------------------------------------------------- /adl/adl_11.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | class C { 3 | //... 4 | }; 5 | auto f = [](C) { 6 | //... 7 | }; 8 | } 9 | 10 | int main() 11 | { 12 | nec::C x; 13 | f(x); //invalid - NO ADL 14 | } 15 | -------------------------------------------------------------------------------- /nested_namespace/nested_01.cpp: -------------------------------------------------------------------------------- 1 | namespace A { 2 | //... 3 | namespace B { 4 | //... 5 | namespace C { 6 | int x; 7 | } 8 | } 9 | } 10 | 11 | int main() 12 | { 13 | A::B::C::x++; 14 | } 15 | -------------------------------------------------------------------------------- /using_directive/using_directive_03.cpp: -------------------------------------------------------------------------------- 1 | // using namespace directive may not be in class scope 2 | 3 | namespace nec { 4 | 5 | } 6 | 7 | class Myclass { 8 | //... 9 | using namespace nec; //error 10 | }; 11 | -------------------------------------------------------------------------------- /adl/adl_16.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | class Myclass { }; 3 | void foo(Myclass); 4 | void bar(Myclass); 5 | } 6 | 7 | 8 | int main() 9 | { 10 | int bar{}; 11 | nec::Myclass m; 12 | 13 | foo(m); 14 | bar(m); // error 15 | } 16 | -------------------------------------------------------------------------------- /adl/adl_15.cpp: -------------------------------------------------------------------------------- 1 | namespace A { 2 | class Myclass { }; 3 | class Functor { 4 | public: 5 | void operator()(Myclass); 6 | }; 7 | } 8 | 9 | 10 | int main() 11 | { 12 | A::Myclass m; 13 | 14 | Functor{}(m); //error no ADL 15 | } 16 | -------------------------------------------------------------------------------- /basic/basic_03.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | class C { 3 | friend void f1(); 4 | friend void f2(const C&); 5 | //... 6 | }; 7 | } 8 | 9 | int main() 10 | { 11 | nec::C cx; 12 | 13 | //f1(); //invalid 14 | f2(cx); // ADL 15 | } 16 | -------------------------------------------------------------------------------- /unnamed_namespace/un_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace 4 | { 5 | int x = 5; 6 | } 7 | 8 | int x = 34; 9 | 10 | int main() 11 | { 12 | std::cout << ::x << '\n'; //valid 13 | //std::cout << x << '\n'; //error 14 | } 15 | -------------------------------------------------------------------------------- /basic/basic_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec 4 | { 5 | void foo(); 6 | //... 7 | } 8 | 9 | void nec::foo() // valid 10 | { 11 | std::cout << "nec::foo()\n"; 12 | } 13 | 14 | int main() 15 | { 16 | nec::foo(); 17 | } 18 | -------------------------------------------------------------------------------- /inline_namespace/il_05_2.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | int x = 10; 3 | inline namespace erg{ 4 | int x = 5; 5 | } 6 | } 7 | 8 | #include 9 | 10 | int main() 11 | { 12 | std::cout << nec::x << '\n'; //error (ambiguity) 13 | } 14 | -------------------------------------------------------------------------------- /inline_namespace/il_05_1.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | int x = 10; 3 | namespace erg { 4 | int x = 5; 5 | } 6 | using namespace erg; 7 | } 8 | 9 | #include 10 | 11 | int main() 12 | { 13 | std::cout << nec::x << '\n'; //10 14 | } 15 | -------------------------------------------------------------------------------- /basic/basic_02.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | int x = 10; 3 | } 4 | 5 | namespace nec { 6 | int y = 21; 7 | } 8 | 9 | namespace nec { 10 | void foo(); 11 | } 12 | 13 | int main() 14 | { 15 | nec::x = 25; 16 | nec::y = 35; 17 | nec::foo(); 18 | } 19 | -------------------------------------------------------------------------------- /adl/adl_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | std::vector vec{ 3, 5, 1, 6, 9, 7, 2, 4 }; 7 | 8 | sort(begin(vec), end(vec)); 9 | //burada hem begin ve end isimleri hem de sort ismi ADL ile bulunuyor. 10 | } 11 | -------------------------------------------------------------------------------- /using_declaration/ud_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec{ 4 | void foo() 5 | { 6 | std::cout << "nec::foo()\n"; 7 | } 8 | } 9 | 10 | 11 | namespace erg { 12 | using nec::foo; 13 | } 14 | 15 | int main() 16 | { 17 | erg::foo(); 18 | } 19 | -------------------------------------------------------------------------------- /basic/basic_01.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | int x; 3 | void foo(); 4 | class Myclass{}; 5 | } 6 | 7 | int main() 8 | { 9 | nec::x = 10; 10 | nec::foo(); 11 | nec::Myclass m1; 12 | 13 | ++x; //invalid 14 | foo(); //invalid 15 | Myclass m2; //invalid 16 | } 17 | -------------------------------------------------------------------------------- /namespace_alias/na_01.cpp: -------------------------------------------------------------------------------- 1 | //namespace alias 2 | 3 | namespace nec_xyz_proj { 4 | int x = 0; 5 | class Erg{ 6 | //... 7 | }; 8 | } 9 | 10 | int main() 11 | { 12 | namespace nec = nec_xyz_proj; 13 | 14 | nec::x = 10; 15 | nec::Erg e{}; 16 | //... 17 | } 18 | -------------------------------------------------------------------------------- /namespace_alias/na_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | namespace rng = std::ranges; 8 | int a[]{5, 2, 6, 1, 4}; 9 | 10 | rng::sort(a); 11 | 12 | for (int i : a) 13 | std::cout << i; 14 | } 15 | -------------------------------------------------------------------------------- /namespace_alias/na_02.cpp: -------------------------------------------------------------------------------- 1 | //namespace alias 2 | 3 | namespace nec { 4 | namespace erg { 5 | namespace proj { 6 | int x = 5; 7 | //... 8 | } 9 | } 10 | } 11 | 12 | int main() 13 | { 14 | namespace proj = nec::erg::proj; 15 | 16 | proj::x = 10; 17 | } 18 | -------------------------------------------------------------------------------- /using_directive/using_directive_13.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec 4 | { 5 | int x = 10; 6 | //... 7 | } 8 | 9 | namespace erg 10 | { 11 | using namespace nec; 12 | //... 13 | } 14 | 15 | int main() 16 | { 17 | std::cout << erg::x << '\n'; 18 | } 19 | -------------------------------------------------------------------------------- /namespace_alias/na_05.cpp: -------------------------------------------------------------------------------- 1 | namespace very_long_namespace_name::project::details { 2 | void func(); 3 | } 4 | 5 | namespace details = very_long_namespace_name::project::details; 6 | 7 | using details::func; 8 | 9 | int main() 10 | { 11 | func(); // valid 12 | } 13 | -------------------------------------------------------------------------------- /basic/basic_04.cpp: -------------------------------------------------------------------------------- 1 | namespace Nec { 2 | 3 | void foo(); 4 | 5 | void bar() 6 | { 7 | foo(); 8 | //baz(); //invalid 9 | //Nec::baz(); //invalid 10 | //x++; //invalid 11 | //Nec::x++; //invalid 12 | } 13 | 14 | void baz(); 15 | int x = 10; 16 | } 17 | -------------------------------------------------------------------------------- /using_directive/using_directive_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec { 4 | //... 5 | } 6 | 7 | namespace erg { 8 | //... 9 | } 10 | 11 | namespace asl { 12 | //... 13 | } 14 | 15 | 16 | using namespace nec, erg, asl; //comma separated list is not allowed 17 | -------------------------------------------------------------------------------- /namespace_alias/na_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace fstd = std::filesystem; 5 | namespace fboost = boost::filesystem; 6 | 7 | int main() 8 | { 9 | fstd::path p1{ "necati.txt" }; 10 | fboost::path p2{ "ergin.txt" }; 11 | } 12 | -------------------------------------------------------------------------------- /using_directive/using_directive_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace erg 4 | { 5 | int x = 10; 6 | } 7 | 8 | int x = 20; 9 | 10 | int main() 11 | { 12 | int x = 100; 13 | 14 | using namespace erg; 15 | 16 | std::cout << "x = " << x << '\n'; //100 - name hiding 17 | } 18 | -------------------------------------------------------------------------------- /nested_namespace/nested_07.cpp: -------------------------------------------------------------------------------- 1 | namespace A 2 | { 3 | namespace B 4 | { 5 | int x = 5; 6 | } 7 | } 8 | 9 | namespace B 10 | { 11 | int y = 78; 12 | } 13 | 14 | int main() 15 | { 16 | auto i1 = A::B::x; //ok 17 | //auto i2 = B::x; //error 18 | //auto i3 = A::B::y; //error 19 | } 20 | -------------------------------------------------------------------------------- /unnamed_namespace/un_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace 4 | { 5 | int x; 6 | 7 | void foo() 8 | { 9 | std::cout << "foo()\n"; 10 | } 11 | } 12 | 13 | int main() 14 | { 15 | x = 10; 16 | foo(); 17 | 18 | std::cout << x << '\n'; 19 | ::x = 5; 20 | ::foo(); 21 | } 22 | -------------------------------------------------------------------------------- /using_directive/using_directive_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace A 4 | { 5 | namespace B 6 | { 7 | int x = 5; 8 | } 9 | 10 | using namespace B; 11 | //... 12 | } 13 | 14 | int main() 15 | { 16 | std::cout << A::x << '\n'; 17 | // std::cout << x << '\n'; //invalid 18 | } 19 | -------------------------------------------------------------------------------- /unnamed_namespace/un_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace 4 | { 5 | int x = 5; 6 | } 7 | 8 | namespace 9 | { 10 | int y = 3; 11 | } 12 | 13 | namespace { 14 | int z = 1; 15 | } 16 | 17 | int main() 18 | { 19 | x += y * z; 20 | 21 | std::cout << "x = " << x << '\n'; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /using_declaration/ud_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace foo { 4 | void f() 5 | { 6 | std::cout << "foo::f()\n"; 7 | } 8 | } 9 | 10 | namespace bar { 11 | using foo::f; 12 | } 13 | 14 | namespace baz { 15 | using bar::f; 16 | } 17 | 18 | int main() 19 | { 20 | baz::f(); 21 | } 22 | -------------------------------------------------------------------------------- /using_declaration/ud_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec { 4 | namespace pro { 5 | void foo() 6 | { 7 | std::cout << "nec::foo()\n"; 8 | } 9 | } 10 | } 11 | 12 | 13 | namespace erg { 14 | using nec::pro::foo; 15 | } 16 | 17 | int main() 18 | { 19 | erg::foo(); 20 | } 21 | -------------------------------------------------------------------------------- /adl/adl_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace A { 5 | class B{}; 6 | void func(std::vector) 7 | { 8 | std::cout << "A::func(std::vector)\n"; 9 | } 10 | } 11 | 12 | int main() 13 | { 14 | std::vector x; 15 | 16 | func(x); //ADL works here 17 | } 18 | -------------------------------------------------------------------------------- /nested_namespace/nested_10.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec 4 | { 5 | namespace erg 6 | { 7 | void foo(); 8 | //... 9 | } 10 | 11 | void erg::foo() // valid 12 | { 13 | std::cout << "nec::erg::foo()\n"; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | nec::erg::foo(); 20 | } 21 | -------------------------------------------------------------------------------- /nested_namespace/nested_11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec 4 | { 5 | namespace erg 6 | { 7 | void foo(); 8 | //... 9 | } 10 | } 11 | 12 | void nec::erg::foo() // valid 13 | { 14 | std::cout << "nec::erg::foo()\n"; 15 | } 16 | 17 | int main() 18 | { 19 | nec::erg::foo(); 20 | } 21 | -------------------------------------------------------------------------------- /nested_namespace/nested_03.cpp: -------------------------------------------------------------------------------- 1 | //C++17 2 | 3 | namespace A::B::C { 4 | int c{ 1 }; 5 | } 6 | 7 | namespace A::B { 8 | int b{ 2 }; 9 | } 10 | 11 | 12 | namespace A { 13 | int a{ 3 }; 14 | } 15 | 16 | #include 17 | 18 | int main() 19 | { 20 | std::cout << A::a << A::B::b << A::B::C::c; 21 | } 22 | -------------------------------------------------------------------------------- /nested_namespace/nested_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec 4 | { 5 | int x = 3; 6 | 7 | namespace nested 8 | { 9 | int x = 5; 10 | 11 | void foo() 12 | { 13 | std::cout << x << '\n'; 14 | std::cout << nec::x << '\n'; 15 | } 16 | } 17 | } 18 | 19 | 20 | int main() 21 | { 22 | nec::nested::foo(); 23 | } 24 | -------------------------------------------------------------------------------- /inline_namespace/il_01.cpp: -------------------------------------------------------------------------------- 1 | namespace A { 2 | inline namespace B { 3 | inline namespace C { 4 | int x = 10; 5 | } 6 | } 7 | 8 | inline namespace D { 9 | int y = 20; 10 | } 11 | } 12 | 13 | int main() 14 | { 15 | A::x = 1; 16 | A::B::x = 2; 17 | A::B::C::x = 3; 18 | A::y = 4; 19 | A::D::y = 5; 20 | } 21 | -------------------------------------------------------------------------------- /using_directive/using_directive_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace X 4 | { 5 | namespace Y 6 | { 7 | namespace Z 8 | { 9 | using namespace std; 10 | 11 | void foo() 12 | { 13 | cout << "X::Y::Z::foo\n"; //valid 14 | } 15 | } 16 | } 17 | } 18 | 19 | int main() 20 | { 21 | // cout << "this is a test\n"; //invalid 22 | } 23 | -------------------------------------------------------------------------------- /overloading/overloading_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace A { 4 | void f(int) 5 | { 6 | std::cout << "A::f(int)\n"; 7 | } 8 | } 9 | 10 | namespace B { 11 | void f() 12 | { 13 | std::cout << "B::f()\n"; 14 | } 15 | } 16 | 17 | int main() 18 | { 19 | using A::f; 20 | using B::f; 21 | f(12); //valid A::f 22 | f(); //valid B::f 23 | } 24 | -------------------------------------------------------------------------------- /using_directive/using_directive_04.cpp: -------------------------------------------------------------------------------- 1 | // using namespace directive for nested namespace 2 | 3 | namespace nec { 4 | namespace erg { 5 | namespace pro { 6 | struct A { 7 | static void foo(); 8 | }; 9 | 10 | void bar(); 11 | } 12 | } 13 | } 14 | 15 | void f() 16 | { 17 | using namespace nec::erg::pro; 18 | A::foo(); 19 | bar(); 20 | } 21 | -------------------------------------------------------------------------------- /using_directive/using_directive_07.cpp: -------------------------------------------------------------------------------- 1 | namespace nec 2 | { 3 | int a; 4 | int b; 5 | } 6 | 7 | namespace erg 8 | { 9 | int a; 10 | int c; 11 | } 12 | 13 | using namespace nec; 14 | using namespace erg; 15 | 16 | int main() 17 | { 18 | //a = 10; // error - ambiguity 19 | nec::a = 20; 20 | erg::a = 30; 21 | b = 20; // ok 22 | c = 30; // ok 23 | } 24 | -------------------------------------------------------------------------------- /adl/adl_14.cpp: -------------------------------------------------------------------------------- 1 | struct Nec { 2 | Nec(int) {} 3 | friend void foo(Nec) {}; 4 | }; 5 | 6 | int main() 7 | { 8 | Nec nec{ 34 }; 9 | foo(nec); // ADL, calls foo defined in friend declaration 10 | foo(99); // Error: foo not found, as int is not Nec 11 | ::foo(nec); // Error: foo not found as ADL not triggered with qualifed name 12 | } 13 | -------------------------------------------------------------------------------- /using_directive/using_directive_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec 4 | { 5 | int x = 10; 6 | } 7 | 8 | int x = 20; 9 | 10 | int main() 11 | { 12 | using namespace nec; 13 | 14 | //std::cout << x << '\n'; //error - ambiguous 15 | std::cout << ::x << '\n'; //valid, x in global namespace 16 | std::cout << nec::x << '\n'; //valid, x in nec namespace 17 | } 18 | -------------------------------------------------------------------------------- /using_directive/using_directive_12.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace A 4 | { 5 | namespace B 6 | { 7 | namespace C 8 | { 9 | int x = 5; 10 | } 11 | } 12 | 13 | using namespace B::C; 14 | //... 15 | } 16 | 17 | 18 | int main() 19 | { 20 | std::cout << A::x; //valid 21 | //std::cout << A::B::x; //invalid 22 | std::cout << A::B::C::x; //valid 23 | } 24 | -------------------------------------------------------------------------------- /adl/adl_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace Nec { 4 | struct X{}; 5 | void func(X) { std::cout << "Nec::func(X)\n"; } 6 | } 7 | 8 | class Myclass { 9 | public: 10 | void func(Nec::X) { std::cout << "Myclass::func(Nec::X)\n";} 11 | 12 | void foo() 13 | { 14 | Nec::X ax; 15 | func(ax); //no ADL 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | Myclass mx; 22 | mx.foo(); 23 | } 24 | -------------------------------------------------------------------------------- /adl/adl_12.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | class NA { 3 | 4 | }; 5 | void foo(int); 6 | void bar(NA); 7 | } 8 | 9 | namespace erg { 10 | using EB = nec::NA; 11 | void func(EB); 12 | } 13 | 14 | int main() 15 | { 16 | // foo(0); //invalid No ADL 17 | nec::foo(0); // valid, no ADL 18 | erg::EB b; 19 | //func(b); //invalid, there is no func in nec 20 | bar(b); //valid, bar found in nec 21 | } 22 | -------------------------------------------------------------------------------- /nested_namespace/nested_08.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void foo() 4 | { 5 | std::cout << "::foo" << '\n'; 6 | } 7 | 8 | namespace nec 9 | { 10 | void foo() 11 | { 12 | std::cout << "nec::foo" << '\n'; 13 | } 14 | 15 | void bar() 16 | { 17 | foo(); // nec::foo 18 | ::foo(); // global foo 19 | } 20 | } 21 | 22 | int main() 23 | { 24 | nec::bar(); 25 | foo(); 26 | ::foo(); 27 | } 28 | -------------------------------------------------------------------------------- /nested_namespace/nested_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace A 4 | { 5 | namespace B 6 | { 7 | int x = 5; 8 | } 9 | } 10 | 11 | namespace A { 12 | namespace B 13 | { 14 | int y = 7; 15 | } 16 | } 17 | 18 | namespace A::B { 19 | int z = 9; 20 | } 21 | 22 | int main() 23 | { 24 | std::cout << A::B::x << '\n'; 25 | std::cout << A::B::y << '\n'; 26 | std::cout << A::B::z << '\n'; 27 | } 28 | -------------------------------------------------------------------------------- /adl/adl_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace Enc { 4 | struct S {}; 5 | void foo(S) 6 | { 7 | std::cout << "Enc::foo(S)\n"; 8 | } 9 | 10 | namespace Nested { 11 | void foo() 12 | { 13 | std::cout << "Enc::Nested::foo()\n"; 14 | } 15 | 16 | void func() 17 | { 18 | S ss; 19 | foo(ss); 20 | } 21 | } 22 | } 23 | 24 | int main() 25 | { 26 | Enc::Nested::func(); //valid 27 | } 28 | -------------------------------------------------------------------------------- /using_directive/using_directive_01.cpp: -------------------------------------------------------------------------------- 1 | // using namespace directive has a scope 2 | 3 | namespace nec { 4 | void foo(); 5 | int ival{}; 6 | struct A { 7 | static void bar(); 8 | }; 9 | 10 | } 11 | 12 | using namespace nec; // in global namespace 13 | 14 | void f1() 15 | { 16 | foo(); 17 | ++ival; 18 | A::bar(); 19 | } 20 | 21 | void f2() 22 | { 23 | foo(); 24 | ++ival; 25 | A::bar(); 26 | } 27 | 28 | -------------------------------------------------------------------------------- /using_directive/using_directive_02.cpp: -------------------------------------------------------------------------------- 1 | // using namespace directive has a scope 2 | 3 | namespace nec { 4 | void foo(); 5 | int ival{}; 6 | struct A { 7 | static void bar(); 8 | }; 9 | 10 | } 11 | 12 | void f1() 13 | { 14 | using namespace nec; // in block scope 15 | 16 | foo(); 17 | ++ival; 18 | A::bar(); 19 | } 20 | 21 | void f2() 22 | { 23 | foo(); //error 24 | ++ival; //error 25 | A::bar(); //error 26 | } 27 | 28 | -------------------------------------------------------------------------------- /inline_namespace/il_04.cpp: -------------------------------------------------------------------------------- 1 | //#define USE_INLINE_Y 2 | 3 | namespace X { 4 | #ifdef USE_INLINE_Y 5 | inline 6 | #endif 7 | namespace Y { 8 | const char* func(bool) 9 | { 10 | return "bool"; 11 | } 12 | } 13 | const char* func(int) 14 | { 15 | return "int"; 16 | } 17 | } 18 | 19 | #include 20 | 21 | int main(void) 22 | { 23 | std::cout << X::func(true); 24 | } 25 | 26 | 27 | -------------------------------------------------------------------------------- /overloading/overloading_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void foo(double) 4 | { 5 | std::cout << "::foo(double)\n"; 6 | } 7 | 8 | void foo(unsigned a) 9 | { 10 | std::cout << "::foo(unsigned)\n"; 11 | } 12 | 13 | namespace nec 14 | { 15 | void foo(int) 16 | { 17 | std::cout << "nec::foo(int)\n"; 18 | } 19 | 20 | void bar() 21 | { 22 | foo(4.1); 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | nec::bar(); 29 | foo(1.2); 30 | foo(5u); 31 | } 32 | -------------------------------------------------------------------------------- /nested_namespace/nested_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace Enc { 4 | 5 | void foo(int) 6 | { 7 | std::cout << "Enc::foo(int)\n"; 8 | } 9 | 10 | namespace Nested { 11 | void foo() 12 | { 13 | std::cout << "Enc::Nested::foo()\n"; 14 | } 15 | 16 | void func() 17 | { 18 | //foo(10); //invalid 19 | foo(); 20 | Enc::foo(10); 21 | } 22 | 23 | } 24 | } 25 | 26 | int main() 27 | { 28 | Enc::foo(0); 29 | Enc::Nested::func(); 30 | } 31 | -------------------------------------------------------------------------------- /nested_namespace/nested_04.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec{ 4 | void foo() 5 | { 6 | std::cout << "nec::foo()\n"; 7 | } 8 | namespace erg { 9 | void foo() 10 | { 11 | std::cout << "erg::foo()\n"; 12 | } 13 | 14 | namespace asl { 15 | void foo() 16 | { 17 | std::cout << "asl::foo()\n"; 18 | } 19 | } 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | nec::foo(); 26 | nec::erg::foo(); 27 | nec::erg::asl::foo(); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /adl/adl_01.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | class Duration{ 3 | public: 4 | Duration(int hour, int min, int sec); 5 | }; 6 | void print(const Duration&); 7 | } 8 | 9 | 10 | namespace erg { 11 | class Duration { 12 | public: 13 | Duration(double min); 14 | }; 15 | void print(const Duration&); 16 | } 17 | 18 | int main() 19 | { 20 | nec::Duration x{ 2, 12, 51 }; 21 | erg::Duration y{ 3445.863}; 22 | 23 | print(x); //nec::print 24 | print(y); //erg::print 25 | 26 | } 27 | -------------------------------------------------------------------------------- /adl/adl_13.cpp: -------------------------------------------------------------------------------- 1 | namespace A { 2 | struct A { operator int(); }; 3 | void f(A); 4 | } 5 | namespace B { 6 | void f(int); 7 | void f(double); 8 | void test() { 9 | A::A a; 10 | void (*fp)(int) = f; // OK, no ADL 11 | void (*gp)(A::A) = f; // ERROR, no ADL, fails to find A::f 12 | f(a); // ADL, calls A::f(A) 13 | (&f)(a); // no ADL, calls B::f(int) 14 | (f)(a); // no ADL, calls B::f(int) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /overloading/overloading_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace Nec { 4 | void func(int) 5 | { 6 | std::cout << "void Nec::func(int)\n"; 7 | } 8 | 9 | void func(double) 10 | { 11 | std::cout << "void Nec::func(double)\n"; 12 | } 13 | } 14 | 15 | int func() 16 | { 17 | std::cout << "int func()\n"; 18 | return 1; 19 | } 20 | 21 | int main() 22 | { 23 | using Nec::func; 24 | 25 | //func(); // invalid 26 | func(12); 27 | func(2.5); 28 | ::func(); 29 | } 30 | -------------------------------------------------------------------------------- /overloading/overloading_06.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace lib { 4 | inline namespace v2 { 5 | void process(double) 6 | { 7 | std::cout << "v2::process(double)\n"; 8 | } 9 | } 10 | 11 | inline namespace v1 12 | { 13 | void process(int) { 14 | std::cout << "v1::process(int)\n"; 15 | } 16 | } 17 | } 18 | 19 | 20 | int main() 21 | { 22 | lib::process(42); 23 | lib::process(3.14); 24 | } 25 | -------------------------------------------------------------------------------- /basic/basic_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec 4 | { 5 | int a = 21; 6 | 7 | void foo() 8 | { 9 | std::cout << "nec::foo" << '\n'; 10 | } 11 | } 12 | 13 | namespace erg 14 | { 15 | int a = 43; 16 | 17 | void foo() 18 | { 19 | std::cout << "erg::foo" << '\n'; 20 | } 21 | } 22 | 23 | int main() 24 | { 25 | std::cout << nec::a << '\n'; 26 | std::cout << erg::a << '\n'; 27 | 28 | nec::foo(); 29 | erg::foo(); 30 | } 31 | -------------------------------------------------------------------------------- /using_declaration/ud_03.cpp: -------------------------------------------------------------------------------- 1 | //Names introduced into a namespace scope by a using - declaration can be used just like any other names, 2 | //including qualified lookup from other scopes: 3 | //example from cppreference.com 4 | 5 | void f(); 6 | 7 | namespace A { 8 | void g(); 9 | } 10 | 11 | namespace X { 12 | using ::f; // global f is now visible as ::X::f 13 | using A::g; // A::g is now visible as ::X::g 14 | } 15 | 16 | void h() 17 | { 18 | X::f(); // calls ::f 19 | X::g(); // calls A::g 20 | } 21 | -------------------------------------------------------------------------------- /overloading/overloading_01.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace Nec { 4 | void func(int) 5 | { 6 | std::cout << "void Nec::func(int)\n"; 7 | } 8 | //... 9 | } 10 | 11 | namespace Ali { 12 | void func(double) 13 | { 14 | std::cout << "void Ali::func(double)\n"; 15 | } 16 | //... 17 | } 18 | 19 | //void func(int) 20 | //{ 21 | // std::cout << "void func(int)\n"; 22 | //} 23 | 24 | using namespace Nec; 25 | using namespace Ali; 26 | 27 | int main() 28 | { 29 | func(123); 30 | func(3.4); 31 | } 32 | -------------------------------------------------------------------------------- /inline_namespace/il_02.cpp: -------------------------------------------------------------------------------- 1 | namespace Nec { 2 | namespace Nested { 3 | class C { 4 | //... 5 | }; 6 | //... 7 | } 8 | using namespace Nested; 9 | void func(Nested::C); 10 | } 11 | 12 | int main() 13 | { 14 | Nec::Nested::C x; 15 | func(x); //gecersiz 16 | } 17 | 18 | 19 | /* 20 | namespace Nec { 21 | inline namespace Nested { 22 | class C { 23 | //... 24 | }; 25 | //... 26 | } 27 | void func(Nested::C); 28 | } 29 | */ 30 | -------------------------------------------------------------------------------- /adl/adl_10.cpp: -------------------------------------------------------------------------------- 1 | namespace nec { 2 | class C { 3 | 4 | }; 5 | template 6 | void foo(T); 7 | 8 | template 9 | void bar(C); 10 | } 11 | 12 | 13 | void func() 14 | { 15 | nec::C x; 16 | using nec::bar; 17 | 18 | bar<5>(x); //valid but no ADL 19 | } 20 | 21 | int main() 22 | { 23 | nec::C x; 24 | 25 | foo(x); //ADL finds foo 26 | nec::foo(x); //valid, but no ADL 27 | foo(x); //invalid in C++17 valid since C++20 28 | nec::bar<10>(x); //valid no ADL 29 | bar<0>(x); 30 | } 31 | -------------------------------------------------------------------------------- /adl/adl_03.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | namespace nec { 5 | class Myclass { 6 | private: 7 | int mx = 10; 8 | public: 9 | friend void swap(Myclass&, Myclass&); 10 | }; 11 | 12 | void swap(Myclass& r1, Myclass& r2) 13 | { 14 | std::cout << "nec::swap()\n"; 15 | int temp{ r1.mx }; 16 | r1.mx = r2.mx; 17 | r2.mx = temp; 18 | } 19 | } 20 | 21 | 22 | using namespace std; 23 | 24 | int main() 25 | { 26 | nec::Myclass m1, m2; 27 | 28 | std::swap(m1, m2); 29 | swap(m1, m2); 30 | } 31 | -------------------------------------------------------------------------------- /overloading/overloading_07.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace lib { 4 | inline namespace v1 { 5 | void process(double) 6 | { 7 | std::cout << "v2::process(double)\n"; 8 | } 9 | } 10 | 11 | inline namespace v2 12 | { 13 | void process(int) 14 | { 15 | std::cout << "v1::process(int)\n"; 16 | } 17 | } 18 | } 19 | 20 | 21 | int main() 22 | { 23 | using namespace lib; 24 | 25 | lib::process(42); 26 | lib::process(3.14); 27 | } 28 | -------------------------------------------------------------------------------- /overloading/overloading_05.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | namespace nec 5 | { 6 | void foo(int nec) 7 | { 8 | std::cout << "nec::foo(int)" << '\n'; 9 | } 10 | } 11 | 12 | namespace erg 13 | { 14 | using namespace nec; 15 | 16 | void foo(long nec) 17 | { 18 | std::cout << "erg::foo(long)" << '\n'; 19 | } 20 | } 21 | 22 | void foo(double nec) 23 | { 24 | std::cout << "::foo(double)" << '\n'; 25 | } 26 | 27 | using namespace erg; 28 | 29 | int main() 30 | { 31 | foo(10); // nec::foo 32 | foo(10L); // erg::foo 33 | foo(3.14); // ::foo 34 | } 35 | -------------------------------------------------------------------------------- /using_directive/using_directive_08.cpp: -------------------------------------------------------------------------------- 1 | namespace nec 2 | { 3 | int x; 4 | //... 5 | } 6 | 7 | namespace erg 8 | { 9 | using namespace nec; 10 | //... 11 | } 12 | 13 | using namespace erg; 14 | 15 | int main() 16 | { 17 | x = 10; // valid 18 | } 19 | 20 | // "using namespace erg" direktifi ile erg isim alanı içerisindeki isimler global isim alanında görülür olur 21 | //Böylece isim alanındaki "using namespace nec" direktifi de sanki global isim alanındaymış gibi etkiye sahip olur. 22 | // Bu etki de nec isim alanı içerisindekilerin global isim alanına enjekte edilmesine yol açar. 23 | -------------------------------------------------------------------------------- /adl/adl_07.cpp: -------------------------------------------------------------------------------- 1 | //bu örnek Herb Sutter'in blogundan alınmıştır: 2 | 3 | namespace A { 4 | struct X{}; 5 | struct Y{}; 6 | void f(int); 7 | void g(X); 8 | } 9 | 10 | namespace B { 11 | void f(int i) 12 | { 13 | f(i); // calls B::f (endless recursion) 14 | } 15 | 16 | void g(A::X x) 17 | { 18 | g(x); // Error: ambiguous between B::g (ordinary lookup) and A::g (argument-dependent lookup) 19 | } 20 | 21 | void h(A::Y y) 22 | { 23 | h(y); // calls B::h (endless recursion): ADL examines the A namespace but finds no A::h, so only B::h from ordinary lookup is used 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /nested_namespace/nested_09.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace nec 4 | { 5 | void foo(const char* str) 6 | { 7 | std::cout << "nec::foo const char *\n"; 8 | } 9 | 10 | namespace erg 11 | { 12 | void foo(double nec) 13 | { 14 | std::cout << "nec::erg::foo(double)\n"; 15 | } 16 | 17 | namespace asl 18 | { 19 | void foo(int) 20 | { 21 | std::cout << "nec::erg::asl::foo(int)\n"; 22 | } 23 | 24 | void bar() 25 | { 26 | foo(12.3); // nec::erg::asl::foo 27 | //foo("necati ergin"); // error! 28 | erg::foo(3.4); 29 | nec::foo("necati ergin"); 30 | } 31 | } 32 | } 33 | } 34 | 35 | int main() 36 | { 37 | nec::erg::asl::bar(); 38 | } 39 | -------------------------------------------------------------------------------- /adl/adl_02.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | std::cout << "Necati Ergin\n"; 6 | operator<<(std::cout, "Tekin Aslan\n"); 7 | //std::cout << endl; 8 | endl(std::cout); 9 | //(endl)(std::cout); 10 | } 11 | 12 | 13 | /* 14 | std::cout << "Necati Ergin\n"; 15 | Burada çağrılan operator<< işlevi std isim alanı içinde bildirilmesine karşın isim arama ile bulunuyor. 16 | Fonksiyona gönderilen argüman olan std::cout std isim alanı içinde olduğundan operator<< işlev ismi std isim alanında da aranıyor. 17 | operator<<(std::cout, "Tekin Aslan\n"); 18 | Aynı durum burada da geçerli. operator<< ADL nedeniyle std isim alanında aranıyor. 19 | std::cout << endl; 20 | Burada ADL devreye girmiyor. Çünkü çağrılan işlev endl işlevi değil. endl operator<< işlevine argüman olarak gönderiliyor. 21 | (endl)(std::cout); 22 | Burada da endl fonksiyonu doğrudan çağrılmıyor. ADL devreye girmiyor. 23 | */ 24 | --------------------------------------------------------------------------------