├── GetValueFromPack1.cpp ├── GetValueFromPack2.cpp ├── IfThenElse1.cpp ├── IfThenElse2.cpp ├── IsArray.cpp ├── IsPointer.cpp ├── ObjectGenerator.cpp ├── PackExpansion1.cpp ├── PackExpansion2.cpp ├── PackExpansion3.cpp ├── PolicyBase.cpp ├── README.md ├── Typelist1.cpp ├── Typelist2.cpp ├── Typelist3.cpp ├── Typelist4_Length.cpp ├── Typelist5_TypeAt.cpp ├── Typelist6_Append.cpp ├── Typelist7_MakeCode.cpp ├── Typelist8_MakeCode.cpp ├── Vector.cpp ├── [MOOC]_CPP_TEMPLATE_PROJECT_과제.docx ├── argument.cpp ├── argument_traits.cpp ├── basic_ex01.cpp ├── basic_ex02.cpp ├── basic_ex03.cpp ├── boost.cpp ├── class_template_basic1.cpp ├── class_template_basic2.cpp ├── complex.cpp ├── concept1.cpp ├── concept2.cpp ├── concept3.cpp ├── constexpr.cpp ├── couple.cpp ├── cpp17_type_deduction.cpp ├── crtp1.cpp ├── crtp2.cpp ├── crtp3.cpp ├── decay1.cpp ├── decay2.cpp ├── enable_if.cpp ├── enable_if2.cpp ├── factorial.cpp ├── fold_expression1.cpp ├── fold_expression2.cpp ├── fold_expression3.cpp ├── fold_expression4.cpp ├── friend1.cpp ├── friend2.cpp ├── friend3.cpp ├── friend4.cpp ├── friend5.cpp ├── friend6.cpp ├── generic_copy_constructor1.cpp ├── generic_copy_constructor2.cpp ├── identity.cpp ├── instantiation1.cpp ├── instantiation2.cpp ├── instantiation3.cpp ├── instantiation4.cpp ├── int2type1.cpp ├── int2type2.cpp ├── int2type3.cpp ├── int2type4.cpp ├── int2type5.cpp ├── integral_constant.cpp ├── lazy1.cpp ├── lazy2.cpp ├── lazy3.cpp ├── lib.cpp ├── lib.h ├── main.cpp ├── member_detect_has_resize.cpp ├── member_detect_has_value_type.cpp ├── parameter1.cpp ├── parameter2.cpp ├── parameter3.cpp ├── parameter4.cpp ├── parameter5.cpp ├── parameter6.cpp ├── parameter7.cpp ├── print_tuple.cpp ├── rebind.cpp ├── remove_pointer1.cpp ├── remove_pointer2.cpp ├── result.cpp ├── result_traits.cpp ├── specialization1.cpp ├── specialization2.cpp ├── specialization3.cpp ├── specialization4.cpp ├── specialization5.cpp ├── square.cpp ├── template.cpp ├── template_alias1.cpp ├── template_alias2.cpp ├── template_alias3.cpp ├── template_alias4.cpp ├── template_vs_function.cpp ├── thin_template1.cpp ├── thin_template2.cpp ├── thin_template3.cpp ├── traits_concept.cpp ├── traits_summary.cpp ├── tuple_using_couple.cpp ├── type_deduction1.cpp ├── type_deduction2.cpp ├── type_deduction3.cpp ├── typename.cpp ├── user_define_allocator.cpp ├── using_and_traits.cpp ├── using_traits.cpp ├── value_type1.cpp ├── value_type2.cpp ├── variadic1.cpp ├── variadic2.cpp ├── xget1.cpp ├── xget2.cpp ├── xget3.cpp ├── xtuple1.cpp ├── xtuple2.cpp └── xtuple3.cpp /GetValueFromPack1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/GetValueFromPack1.cpp -------------------------------------------------------------------------------- /GetValueFromPack2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/GetValueFromPack2.cpp -------------------------------------------------------------------------------- /IfThenElse1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/IfThenElse1.cpp -------------------------------------------------------------------------------- /IfThenElse2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/IfThenElse2.cpp -------------------------------------------------------------------------------- /IsArray.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/IsArray.cpp -------------------------------------------------------------------------------- /IsPointer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/IsPointer.cpp -------------------------------------------------------------------------------- /ObjectGenerator.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/ObjectGenerator.cpp -------------------------------------------------------------------------------- /PackExpansion1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/PackExpansion1.cpp -------------------------------------------------------------------------------- /PackExpansion2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : PackExpansion2.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | // Type Expansion 14 | template void foo() 15 | { 16 | // Types : int, char 17 | pair p1; // pair ok 18 | tuple t1; // tuple ok 19 | 20 | tuple> t2; // tuple> 21 | 22 | //pair> p2; // pair< tuple > error 23 | pair...> p3; // pair< tuple, tuple> ok 24 | 25 | //tuple...> t3; // tuple< pair, pair> error 26 | 27 | tuple...> t4; // tuple< pair, pair> ok.. 28 | } 29 | 30 | int main() 31 | { 32 | foo(); 33 | } 34 | -------------------------------------------------------------------------------- /PackExpansion3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/PackExpansion3.cpp -------------------------------------------------------------------------------- /PolicyBase.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/PolicyBase.cpp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TEMPLATE 2 | -------------------------------------------------------------------------------- /Typelist1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/Typelist1.cpp -------------------------------------------------------------------------------- /Typelist2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/Typelist2.cpp -------------------------------------------------------------------------------- /Typelist3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template struct Typelist 5 | { 6 | typedef T Head; 7 | typedef U Tail; 8 | }; 9 | struct NullType {}; 10 | 11 | #define TYPELIST_1(T1) Typelist 12 | #define TYPELIST_2(T1, T2) Typelist> 13 | #define TYPELIST_3(T1, T2, T3) Typelist>> 14 | #define TYPELIST_4(T1, T2, T3, T4) Typelist>>> 15 | 16 | //------------------------------------------- 17 | template class xtuple {}; 18 | 19 | int main() 20 | { 21 | //xtuple t1; 22 | 23 | xtuple< TYPELIST_3(int, double, char) > t1; 24 | 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Typelist4_Length.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/Typelist4_Length.cpp -------------------------------------------------------------------------------- /Typelist5_TypeAt.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/Typelist5_TypeAt.cpp -------------------------------------------------------------------------------- /Typelist6_Append.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template struct Typelist 5 | { 6 | typedef T Head; 7 | typedef U Tail; 8 | }; 9 | struct NullType {}; 10 | 11 | #define TYPELIST_1(T1) Typelist 12 | #define TYPELIST_2(T1, T2) Typelist> 13 | #define TYPELIST_3(T1, T2, T3) Typelist>> 14 | #define TYPELIST_4(T1, T2, T3, T4) Typelist>>> 15 | //------------------------------------------------------------------------------------------- 16 | // Typelist 끝에 타입 추가하기. 17 | 18 | template struct Append; 19 | 20 | 21 | // TL T 22 | // 1. NullType, NullType => NullType 23 | template<> struct Append 24 | { 25 | typedef NullType type; 26 | }; 27 | 28 | // 2. NullType, 임의의타입 => Typelist<임의의타입, NullType> 29 | template struct Append 30 | { 31 | typedef Typelist type; 32 | }; 33 | 34 | // 3. NullType, Typelist => Typelist 35 | template struct Append > 36 | { 37 | typedef Typelist type; 38 | }; 39 | 40 | 41 | // 4. Typelist, NullType => Typelist 42 | // 이번 단계의 코드는 없어도 됩니다. 5단계의 코드만 있으면 됩니다. 43 | template struct Append, NullType > 44 | { 45 | typedef Typelist type; 46 | }; 47 | 48 | 49 | // 5. Typelist, T => Typelist::type> 50 | 51 | template struct Append, T > 52 | { 53 | typedef Typelist::type> type; 54 | }; 55 | 56 | 57 | 58 | 59 | 60 | template void test() 61 | { 62 | typename Append::type t1; 63 | 64 | cout << typeid(t1).name() << endl; // int, char, double, int, NullType 65 | } 66 | 67 | int main() 68 | { 69 | test(); 70 | } 71 | -------------------------------------------------------------------------------- /Typelist7_MakeCode.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/Typelist7_MakeCode.cpp -------------------------------------------------------------------------------- /Typelist8_MakeCode.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/Typelist8_MakeCode.cpp -------------------------------------------------------------------------------- /Vector.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/Vector.cpp -------------------------------------------------------------------------------- /[MOOC]_CPP_TEMPLATE_PROJECT_과제.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/[MOOC]_CPP_TEMPLATE_PROJECT_과제.docx -------------------------------------------------------------------------------- /argument.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/argument.cpp -------------------------------------------------------------------------------- /argument_traits.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/argument_traits.cpp -------------------------------------------------------------------------------- /basic_ex01.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : basic_ex01.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | int square(int a) 10 | { 11 | int ret = a * a; 12 | return ret; 13 | } 14 | 15 | double square(double a) 16 | { 17 | double ret = a * a; 18 | return ret; 19 | } 20 | 21 | int main() 22 | { 23 | square(3); 24 | square(3.3); 25 | } 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /basic_ex02.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : basic_ex02.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | 10 | #define MAKE_SQUARE(T) \ 11 | T square(T a) \ 12 | { \ 13 | T ret = a * a; \ 14 | return ret; \ 15 | } 16 | 17 | MAKE_SQUARE(int) 18 | MAKE_SQUARE(double) 19 | 20 | int main() 21 | { 22 | square(3); 23 | square(3.3); 24 | } -------------------------------------------------------------------------------- /basic_ex03.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/basic_ex03.cpp -------------------------------------------------------------------------------- /boost.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : boost.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | using namespace boost::typeindex; 14 | 15 | template void foo(const T a) 16 | { 17 | // cout << "T : " << typeid(T).name() << endl; 18 | // cout << "T : " << typeid(a).name() << endl; 19 | 20 | cout << "T : " << type_id_with_cvr().pretty_name() << endl; 21 | cout << "a : " << type_id_with_cvr().pretty_name() << endl; 22 | } 23 | 24 | int main() 25 | { 26 | foo(3); // T : int a : const int 27 | foo(3.3); 28 | } -------------------------------------------------------------------------------- /class_template_basic1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/class_template_basic1.cpp -------------------------------------------------------------------------------- /class_template_basic2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/class_template_basic2.cpp -------------------------------------------------------------------------------- /complex.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : complex.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | /* 13 | class complex 14 | { 15 | int re; 16 | int im; 17 | public: 18 | complex(int r, int i) : re(r), im(i) {} 19 | }; 20 | */ 21 | 22 | template class complex 23 | { 24 | T re; 25 | T im; 26 | public: 27 | complex(T r, T i) : re(r), im(i) {} 28 | }; 29 | 30 | int main() 31 | { 32 | complex c1(3, 5); 33 | } -------------------------------------------------------------------------------- /concept1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/concept1.cpp -------------------------------------------------------------------------------- /concept2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : concept2.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | #include 9 | using namespace std; 10 | 11 | struct Point 12 | { 13 | int x, y; 14 | /* 15 | bool operator<(const Point& p) 16 | { 17 | return x < p.x; 18 | } 19 | */ 20 | }; 21 | 22 | template 23 | concept bool LessThanComparable = requires(T a, T b) 24 | { 25 | { a < b } -> bool; 26 | }; 27 | 28 | template requires LessThanComparable 29 | T Min(T x, T y) 30 | { 31 | return (y < x) ? y : x; 32 | } 33 | 34 | int main() 35 | { 36 | Point p1, p2; 37 | Min(p1, p2); 38 | } -------------------------------------------------------------------------------- /concept3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/concept3.cpp -------------------------------------------------------------------------------- /constexpr.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | template struct check {}; 5 | 6 | // constexpr 함수 - C++11 7 | constexpr int add(int a, int b) 8 | { 9 | return a + b; 10 | } 11 | 12 | int main() 13 | { 14 | int n = add(1, 2); 15 | 16 | check< add(1, 2) > c; // ok.. 17 | 18 | 19 | int n1 = 1, n2 = 2; 20 | 21 | int c = add(n1, n2); // ok 22 | 23 | //check< add(n1, n2) > c; // error 24 | } 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /couple.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/couple.cpp -------------------------------------------------------------------------------- /cpp17_type_deduction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // C++17 class template type deduction 5 | 6 | template class Vector 7 | { 8 | public: 9 | Vector(int sz, T value) {} 10 | 11 | template Vector(const C& c) {} 12 | template Vector(IT first, IT second) {} 13 | }; 14 | template Vector(const C& c)->Vector; 15 | template Vector(IT first, IT second)->Vector; 16 | 17 | int main() 18 | { 19 | Vector v1(10, 5); 20 | list s = { 1,2,3 }; 21 | 22 | Vector v2(s); // 23 | Vector v3(s.begin(), s.end()); 24 | 25 | int x[10] = { 1,2,3,4,5,6,7,8,9,10 }; 26 | Vector v4(x, x + 10); // error. 27 | } 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /crtp1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // CRTP : Curiously Reccuring Template Pattern 5 | template class Window 6 | { 7 | public: 8 | void msgLoop() // void msgLoop(Window* this) 9 | { 10 | static_cast(this)->onKeyDown(); 11 | } 12 | void onKeyDown() { cout << "Window onKeyDown" << endl; } 13 | }; 14 | 15 | class MyWindow : public Window 16 | { 17 | public: 18 | void onKeyDown() { cout << "Window onKeyDown" << endl; } 19 | }; 20 | 21 | int main() 22 | { 23 | MyWindow w; 24 | w.msgLoop(); 25 | } -------------------------------------------------------------------------------- /crtp2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : crtp2.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template class Singleton 13 | { 14 | public: 15 | static T* instance; 16 | 17 | static T& getInstance() 18 | { 19 | if (instance == 0) 20 | instance = new T; 21 | return *instance; 22 | } 23 | }; 24 | 25 | template T* Singleton::instance = 0; 26 | 27 | class Cursor : public Singleton 28 | { 29 | }; 30 | 31 | int main() 32 | { 33 | Cursor& c = Cursor::getInstance(); 34 | } -------------------------------------------------------------------------------- /crtp3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : crtp3.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template struct Count 13 | { 14 | private: 15 | static int cnt; 16 | public: 17 | Count() { ++cnt; } 18 | ~Count() { --cnt; } 19 | 20 | static int count() { return cnt; } 21 | }; 22 | template int Count::cnt = 0; 23 | 24 | class Mouse : public Count 25 | { 26 | }; 27 | class Keyboard : public Count 28 | { 29 | }; 30 | int main() 31 | { 32 | Mouse m1, m2; 33 | Keyboard k1, k2; 34 | 35 | cout << k1.count() << endl; 36 | } -------------------------------------------------------------------------------- /decay1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/decay1.cpp -------------------------------------------------------------------------------- /decay2.cpp: -------------------------------------------------------------------------------- 1 | 2 | template void foo(T a, T b) 3 | { 4 | } 5 | 6 | template void goo(T& a, T& b) 7 | { 8 | } 9 | 10 | int main() 11 | { 12 | // "orange" : const char [7] 13 | // "apple" : const char [6] 14 | foo("orange", "apple"); // ok foo( const char*, const char*) 15 | goo("orange", "apple"); // error goo( const char [7], const char [6]) 16 | } 17 | 18 | -------------------------------------------------------------------------------- /enable_if.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/enable_if.cpp -------------------------------------------------------------------------------- /enable_if2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : enable_if2.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | // 포인터 일때와 포인터가 아닐때 14 | /* 15 | template void printv(T a) 16 | { 17 | // 방법 1. if 문 사용.. 18 | if (is_pointer::value) 19 | { 20 | //.... 21 | } 22 | else 23 | { 24 | //.... 25 | // *a = 0; error... 26 | } 27 | } 28 | */ 29 | 30 | // 방법 2. true_type, false_type 으로 오버로딩.. 31 | /* 32 | template void printv_imp(T a, true_type) {} 33 | template void printv_imp(T a, false_type) {} 34 | 35 | template void printv(T a) 36 | { 37 | printv_imp(a, is_pointer()); 38 | } 39 | */ 40 | // 방법 3. enable_if 사용. 41 | template typename enable_if< is_pointer::value, void>::type printv(T a) 42 | { 43 | cout << "포인터 일때" << endl; 44 | } 45 | 46 | template typename enable_if< !is_pointer::value, void>::type printv(T a) 47 | { 48 | cout << "포인터 아닐때" << endl; 49 | } 50 | 51 | 52 | int main() 53 | { 54 | int n = 10; 55 | printv(n); 56 | printv(&n); 57 | } 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /factorial.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : factorial.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | // template meta programming 13 | template struct factorial 14 | { 15 | //int value = 10; 16 | //enum { value = N * factorial::value }; 17 | static constexpr int value = N * factorial::value; 18 | }; 19 | // 재귀의 종료를 위해 특수화 문법 사용 20 | template<> struct factorial<1> 21 | { 22 | //enum { value = 1 }; 23 | static constexpr int value = 1; 24 | }; 25 | 26 | int main() 27 | { 28 | int n = factorial<5>::value; // 5 * 4 * 3 * 2 * 1 => 120 29 | // 5 * f<4>::v 30 | // 4 * f<3>::v 31 | // 3 * f<2>::v 32 | // 2 * f<1>::v 33 | // 1 34 | 35 | cout << n << endl; 36 | } 37 | 38 | -------------------------------------------------------------------------------- /fold_expression1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : fold_expression1.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template void foo(Types ... args) 13 | { 14 | int x[] = { args... }; // PATTERN... 15 | 16 | int n = (args + ...); // pack op ... 17 | // E1 op ( E2 op ( E3 op ( E4 op E5) ) ) 18 | // 1 + (2 + (3 + (4 + 5))) 19 | 20 | cout << n << endl; 21 | } 22 | int main() 23 | { 24 | foo(1, 2, 3, 4, 5); 25 | } -------------------------------------------------------------------------------- /fold_expression2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : fold_expression2.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template void foo(Types ... args) 13 | { 14 | int n1 = (args + ...); // (1 + (2 + (3 + (4 + 5)))) 15 | int n2 = (... + args); // ((((1 + 2) + 3) + 4) + 5) 16 | 17 | int n3 = (args + ... + 10); // (1 + (2 + (3 + (4 + (5 + 10))))) 18 | int n4 = (10 + ... + args); // (((( 10 + 1) + 2) + 3) + 4) + 5) 19 | 20 | cout << n1 << endl; 21 | cout << n2 << endl; 22 | cout << n3 << endl; 23 | cout << n4 << endl; 24 | } 25 | int main() 26 | { 27 | foo(1, 2, 3, 4, 5); 28 | } -------------------------------------------------------------------------------- /fold_expression3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : fold_expression3.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template void foo(Types ... args) 13 | { 14 | // I : initial value 15 | // I + ... + args; // (((I + E1) + E2) + E3) 16 | 17 | (cout << ... << args); // ((( cout << 1) << 2) << 3) 18 | } 19 | int main() 20 | { 21 | foo(1, 2, 3); 22 | 23 | // (((cout << 1) << 2) << 3) << endl; // 123 24 | 25 | } -------------------------------------------------------------------------------- /fold_expression4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : fold_expression4.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | vector v; 14 | 15 | template void foo(Types ... args) 16 | { 17 | int n1 = (args + ...); // (1 + (2 + 3) ) 18 | 19 | (v.push_back(args), ...); // ( v.push_back(1), (v.push_back(2), v.push_back(3))) 20 | 21 | for (auto n : v) 22 | cout << n << endl; 23 | } 24 | 25 | int main() 26 | { 27 | foo(1, 2, 3); 28 | } -------------------------------------------------------------------------------- /friend1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : friend1.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | class Point 13 | { 14 | int x, y; 15 | public: 16 | Point() : x(0), y(0) {} 17 | Point(int a, int b) : x(a), y(b) {} 18 | 19 | friend ostream& operator <<(ostream& os, const Point& p); 20 | }; 21 | 22 | ostream& operator <<(ostream& os, const Point& p) 23 | { 24 | return os << p.x << ", " << p.y; 25 | } 26 | int main() 27 | { 28 | Point p(1, 2); 29 | cout << p << endl; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /friend2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : friend2.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template class Point 13 | { 14 | T x, y; 15 | public: 16 | Point() : x(0), y(0) {} 17 | Point(T a, T b) : x(a), y(b) {} 18 | 19 | friend ostream& operator <<(ostream& os, const Point& p); 20 | }; 21 | 22 | template 23 | ostream& operator <<(ostream& os, const Point& p) 24 | { 25 | return os << p.x << ", " << p.y; 26 | } 27 | 28 | int main() 29 | { 30 | Point p(1, 2); 31 | cout << p << endl; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /friend3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : friend3.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template 13 | void foo(T a) 14 | { 15 | cout << "T" << endl; 16 | } 17 | 18 | void foo(int a); // { cout << "int" << endl; } 19 | 20 | int main() 21 | { 22 | foo(3); // link error. 23 | } 24 | -------------------------------------------------------------------------------- /friend4.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : friend4.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | /* 12 | template class Point 13 | { 14 | T x, y; 15 | public: 16 | Point() : x(0), y(0) {} 17 | Point(T a, T b) : x(a), y(b) {} 18 | friend ostream& operator <<(ostream& os, const Point& p); 19 | }; 20 | */ 21 | // 컴파일러가 생성한 코드.. 22 | class Point 23 | { 24 | int x, y; 25 | public: 26 | Point() : x(0), y(0) {} 27 | Point(int a, int b) : x(a), y(b) {} 28 | friend ostream& operator <<(ostream& os, const Point& p); 29 | }; 30 | 31 | template 32 | ostream& operator <<(ostream& os, const Point& p) 33 | { 34 | return os << p.x << ", " << p.y; 35 | } 36 | 37 | int main() 38 | { 39 | Point p(1, 2); 40 | cout << p;// operator<<(cout, p) => operator<<(ostream, Point ) 41 | } 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /friend5.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : friend5.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | 10 | #include 11 | using namespace std; 12 | // 해결책 1. 13 | // 클래스와 friend 함수 14 | // 1 : 1, 15 | // 1 : N, 16 | // N : 1, 17 | // N : N => 18 | template class Point 19 | { 20 | T x, y; 21 | public: 22 | Point() : x(0), y(0) {} 23 | Point(T a, T b) : x(a), y(b) {} 24 | 25 | // 함수 템플릿이 아님.. 26 | //friend ostream& operator <<(ostream& os, const Point& p); 27 | 28 | // 함수 템플릿. 29 | template 30 | friend ostream& operator <<(ostream& os, const Point& p); 31 | }; 32 | 33 | // 함수 템플릿 34 | template 35 | ostream& operator <<(ostream& os, const Point& p) 36 | { 37 | return os << p.x << ", " << p.y; 38 | } 39 | 40 | int main() 41 | { 42 | Point p(1, 2); 43 | cout << p << endl; 44 | } 45 | 46 | -------------------------------------------------------------------------------- /friend6.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : friend6.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | // 1 : 1 의 관계.. 13 | template class Point 14 | { 15 | T x, y; 16 | public: 17 | Point() : x(0), y(0) {} 18 | Point(T a, T b) : x(a), y(b) {} 19 | 20 | // friend 함수 구현을 클래스 내부에.. 21 | friend ostream& operator <<(ostream& os, const Point& p) 22 | { 23 | return os << p.x << ", " << p.y; 24 | } 25 | }; 26 | 27 | int main() 28 | { 29 | Point p(1, 2); 30 | cout << p << endl; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /generic_copy_constructor1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/generic_copy_constructor1.cpp -------------------------------------------------------------------------------- /generic_copy_constructor2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/generic_copy_constructor2.cpp -------------------------------------------------------------------------------- /identity.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/identity.cpp -------------------------------------------------------------------------------- /instantiation1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : instantiation1.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | template T square(T a) 10 | { 11 | T ret = a * a; 12 | return ret; 13 | } 14 | 15 | int main() 16 | { 17 | // explicit instantiation 18 | square(3); 19 | square(3.4); 20 | 21 | // implicit instantiation 22 | square(3); 23 | square(3.4); 24 | } -------------------------------------------------------------------------------- /instantiation2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/instantiation2.cpp -------------------------------------------------------------------------------- /instantiation3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/instantiation3.cpp -------------------------------------------------------------------------------- /instantiation4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/instantiation4.cpp -------------------------------------------------------------------------------- /int2type1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/int2type1.cpp -------------------------------------------------------------------------------- /int2type2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/int2type2.cpp -------------------------------------------------------------------------------- /int2type3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/int2type3.cpp -------------------------------------------------------------------------------- /int2type4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/int2type4.cpp -------------------------------------------------------------------------------- /int2type5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/int2type5.cpp -------------------------------------------------------------------------------- /integral_constant.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/integral_constant.cpp -------------------------------------------------------------------------------- /lazy1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/lazy1.cpp -------------------------------------------------------------------------------- /lazy2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/lazy2.cpp -------------------------------------------------------------------------------- /lazy3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/lazy3.cpp -------------------------------------------------------------------------------- /lib.cpp: -------------------------------------------------------------------------------- 1 | // lib.cpp 2 | 3 | void foo(int a) 4 | { 5 | } 6 | 7 | template T square(T a) 8 | { 9 | T ret = a * a; 10 | return ret; 11 | } -------------------------------------------------------------------------------- /lib.h: -------------------------------------------------------------------------------- 1 | // lib.h 2 | 3 | void foo(int); 4 | 5 | template 6 | T square(T a); -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | // main.cpp 2 | #include "lib.h" 3 | 4 | int main() 5 | { 6 | foo(3); 7 | square(3); 8 | } -------------------------------------------------------------------------------- /member_detect_has_resize.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/member_detect_has_resize.cpp -------------------------------------------------------------------------------- /member_detect_has_value_type.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : member_detect_has_value_type.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | struct NoValueType 13 | { 14 | }; 15 | 16 | struct HasValueType 17 | { 18 | typedef int value_type; 19 | }; 20 | 21 | template struct has_value_type 22 | { 23 | using YES = char; 24 | using NO = short; 25 | 26 | template static YES check(typename U::value_type* a); 27 | template static NO check(...); 28 | 29 | static constexpr bool value = (sizeof(check(0)) == sizeof(YES)); 30 | }; 31 | 32 | int main() 33 | { 34 | cout << has_value_type::value << endl; // 1 35 | cout << has_value_type::value << endl; // 0 36 | } 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /parameter1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : parameter1.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | // 1. type 10 | // 2. 값( non-type) 11 | // 3. template 12 | 13 | template struct Stack 14 | { 15 | T buff[N]; 16 | }; 17 | 18 | int main() 19 | { 20 | Stack s; 21 | } 22 | -------------------------------------------------------------------------------- /parameter2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : parameter2.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | // type parameter 10 | template class List 11 | { 12 | }; 13 | 14 | int main() 15 | { 16 | list s1; 17 | } -------------------------------------------------------------------------------- /parameter3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/parameter3.cpp -------------------------------------------------------------------------------- /parameter4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/parameter4.cpp -------------------------------------------------------------------------------- /parameter5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/parameter5.cpp -------------------------------------------------------------------------------- /parameter6.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/parameter6.cpp -------------------------------------------------------------------------------- /parameter7.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/parameter7.cpp -------------------------------------------------------------------------------- /print_tuple.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : print_tuple.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | #include 11 | using namespace std; 12 | 13 | // template struct index_sequence {}; 14 | 15 | template 16 | void print_tuple_imp(const TP& tp, const index_sequence& ) // I : 0, 1, 2 17 | { 18 | int x[] = { get(tp)... }; // get<0>(tp), get<1>(tp), get<2>(tp) 19 | 20 | for (auto& n : x) 21 | cout << n << ", "; 22 | } 23 | 24 | template void print_tuple(const TP& tp) 25 | { 26 | print_tuple_imp(tp, make_index_sequence::value>()); 27 | } 28 | 29 | int main() 30 | { 31 | tuple tp(1, 2, 3); 32 | 33 | print_tuple(tp); 34 | } 35 | -------------------------------------------------------------------------------- /rebind.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : rebind.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | // 실제 allocator 소스는 user_define_allocator.cpp 소스를 참고 하세요. 10 | template class allocator 11 | { 12 | public: 13 | T* allocate(int sz) { return new T[sz]; } 14 | void deallocate(T* p) { delete[] p; } 15 | 16 | // 17 | template struct rebind 18 | { 19 | typedef allocator other; 20 | }; 21 | }; 22 | // rebind 23 | template > class list 24 | { 25 | struct NODE { T data; NODE *next, *prev; }; 26 | 27 | //Ax ax; // allocator 28 | //allocator::rebind::other ax; // allocator ax; 29 | typename Ax::template rebind::other ax; // allocator ax; 30 | 31 | public: 32 | void push_front(const T& a) 33 | { 34 | ax.allocate(1); 35 | } 36 | }; 37 | 38 | int main() 39 | { 40 | list s; 41 | s.push_front(10); 42 | 43 | } 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /remove_pointer1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/remove_pointer1.cpp -------------------------------------------------------------------------------- /remove_pointer2.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : remove_pointer2.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template struct xremove_pointer 13 | { 14 | typedef T type; 15 | }; 16 | 17 | /* 18 | template struct xremove_pointer // int** * 19 | { 20 | typedef T type; 21 | }; 22 | */ 23 | 24 | template struct xremove_pointer // int** * 25 | { 26 | typedef typename xremove_pointer::type type; 27 | }; 28 | 29 | int main() 30 | { 31 | xremove_pointer::type n; 32 | 33 | cout << typeid(n).name() << endl; 34 | } 35 | 36 | -------------------------------------------------------------------------------- /result.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/result.cpp -------------------------------------------------------------------------------- /result_traits.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/result_traits.cpp -------------------------------------------------------------------------------- /specialization1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/specialization1.cpp -------------------------------------------------------------------------------- /specialization2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/specialization2.cpp -------------------------------------------------------------------------------- /specialization3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/specialization3.cpp -------------------------------------------------------------------------------- /specialization4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/specialization4.cpp -------------------------------------------------------------------------------- /specialization5.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/specialization5.cpp -------------------------------------------------------------------------------- /square.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : square.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | // cl square.cpp /FAs => square.asm 10 | // g++ square.cpp -S => square.s 11 | // g++ -fdump-tree-original square.cpp =>square.cpp.003t.original 12 | 13 | 14 | template 15 | T square(T a) 16 | { 17 | T ret = a * a; 18 | return ret; 19 | } 20 | 21 | int main() 22 | { 23 | square(3); 24 | square(3.3); 25 | } -------------------------------------------------------------------------------- /template.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/template.cpp -------------------------------------------------------------------------------- /template_alias1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : template_alias1.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | // template alias ( using ) 10 | 11 | //typedef int DWORD; 12 | //typedef void(*F)(); 13 | 14 | using DWORD = int; 15 | using F = void(*)(); 16 | 17 | 18 | int main() 19 | { 20 | DWORD n; // int 21 | F f; // void(*)() 22 | } 23 | -------------------------------------------------------------------------------- /template_alias2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/template_alias2.cpp -------------------------------------------------------------------------------- /template_alias3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/template_alias3.cpp -------------------------------------------------------------------------------- /template_alias4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/template_alias4.cpp -------------------------------------------------------------------------------- /template_vs_function.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : template_vs_function.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template 13 | T square(T a) 14 | { 15 | T ret = a * a; 16 | return a; 17 | } 18 | void foo() 19 | { 20 | } 21 | 22 | int main() 23 | { 24 | printf("%p\n", &foo); 25 | //printf("%p\n", &square); // ? 26 | //printf("%p\n", &square); // ? 27 | printf("%p\n", static_cast(&square)); // ? 28 | } -------------------------------------------------------------------------------- /thin_template1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/thin_template1.cpp -------------------------------------------------------------------------------- /thin_template2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/thin_template2.cpp -------------------------------------------------------------------------------- /thin_template3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/thin_template3.cpp -------------------------------------------------------------------------------- /traits_concept.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : traits_concept.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | #include 10 | using namespace std; 11 | 12 | template void printv(T a) 13 | { 14 | if (T is Pointer) 15 | cout << a << " : " << *a << endl; 16 | else 17 | cout << a << endl; 18 | } 19 | 20 | 21 | int main() 22 | { 23 | int n = 3; 24 | 25 | printv(n); 26 | printv(&n); 27 | } 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /traits_summary.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/traits_summary.cpp -------------------------------------------------------------------------------- /tuple_using_couple.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/tuple_using_couple.cpp -------------------------------------------------------------------------------- /type_deduction1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/type_deduction1.cpp -------------------------------------------------------------------------------- /type_deduction2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/type_deduction2.cpp -------------------------------------------------------------------------------- /type_deduction3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/type_deduction3.cpp -------------------------------------------------------------------------------- /typename.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/typename.cpp -------------------------------------------------------------------------------- /user_define_allocator.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/user_define_allocator.cpp -------------------------------------------------------------------------------- /using_and_traits.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/using_and_traits.cpp -------------------------------------------------------------------------------- /using_traits.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/using_traits.cpp -------------------------------------------------------------------------------- /value_type1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/value_type1.cpp -------------------------------------------------------------------------------- /value_type2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/value_type2.cpp -------------------------------------------------------------------------------- /variadic1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/variadic1.cpp -------------------------------------------------------------------------------- /variadic2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/variadic2.cpp -------------------------------------------------------------------------------- /xget1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/xget1.cpp -------------------------------------------------------------------------------- /xget2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/xget2.cpp -------------------------------------------------------------------------------- /xget3.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * HOME : ecourse.co.kr 3 | * EMAIL : smkang @ codenuri.co.kr 4 | * COURSENAME : C++ Template Programming 5 | * MODULE : xget3.cpp 6 | * Copyright (C) 2017 CODENURI Inc. All rights reserved. 7 | */ 8 | 9 | 10 | #include 11 | using namespace std; 12 | 13 | // xtuple 14 | template struct xtuple 15 | { 16 | static constexpr int N = 0; 17 | }; 18 | 19 | template 20 | struct xtuple : public xtuple 21 | { 22 | T value; 23 | xtuple() = default; 24 | xtuple(const T& v, const Types& ... args) : value(v), xtuple(args...) {} 25 | static constexpr int N = xtuple::N + 1; 26 | }; 27 | 28 | // xtuple_element_type 29 | template struct xtuple_element_type; 30 | 31 | template 32 | struct xtuple_element_type<0, xtuple> 33 | { 34 | typedef T type; 35 | typedef xtuple tupleType; 36 | }; 37 | 38 | template 39 | struct xtuple_element_type> 40 | { 41 | typedef typename xtuple_element_type>::type type; 42 | typedef typename xtuple_element_type>::tupleType tupleType; 43 | }; 44 | 45 | // get 46 | template typename xtuple_element_type::type& xget(T& tp) 47 | { 48 | return static_cast::tupleType&>(tp).value; 49 | } 50 | 51 | int main() 52 | { 53 | xtuple t3(1, 3.4, 'A'); 54 | 55 | xget<0>(t3) = 10; 56 | 57 | cout << xget<0>(t3) << endl; // 10 58 | cout << xget<1>(t3) << endl; // 3.4 59 | cout << xget<2>(t3) << endl; // 'A' 60 | } 61 | -------------------------------------------------------------------------------- /xtuple1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // Step 1. 가변인자 템플릿 5 | template struct xtuple 6 | { 7 | static constexpr int N = 0; 8 | }; 9 | 10 | int main() 11 | { 12 | xtuple<> t0; 13 | xtuple t1; 14 | xtuple t2; 15 | xtuple t3; 16 | 17 | } 18 | 19 | -------------------------------------------------------------------------------- /xtuple2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/xtuple2.cpp -------------------------------------------------------------------------------- /xtuple3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codenuri/TEMPLATE/4fab4921b8cddbb5a8f3b202a3820d1dae9acb87/xtuple3.cpp --------------------------------------------------------------------------------