반응형

함수 찾는 순서

  • 1순위 정확한 매칭(exactly matching) 
  • 2순위 템플릿(template)
  • 3순위 가변 인자(variable argument)
#include <iostream>
using namespace std;

template<typename T>
// 2순위 템플릿(template)
void foo(T t) { cout << "T" << endl; }
// 1순위 정확한 매칭(exactly matching)
void foo(int n) { cout << "int" << endl; }
// 3순위 가변 인자(variable argument)
void foo(...) { cout << "..." << endl; }

int main()
{
    foo(3);
}

 

SFINAE

  • Substitution Failure Is Not An Error

  • 함수 템플릿을 사용시 T의 타입이 결정되고 함수를 생성(Instantiation)하려고 할때 리턴 타입이나 함수 인자등에서 치환에 실패하면 컴파일 에러가 아니라, 함수 후보군에서 제외함

  • 동일한 이름의 다른 함수가 있다면 다른 함수를 사용하게 됨

#include <iostream>
using namespace std;

template<typename T>
typename T::type foo(T t) 
{ 
    cout << "T" << endl; return 0; 
}

void foo(...) 
{ 
    cout << "..." << endl; 
}

int main()
{
    // 가변인자 함수보다 템플릿 함수가 우선순위가 높아서 먼저 참조하게 되지만 리턴 타입에서 
    // int 타입::type은 존재하지 않으므로 만족할 수 없으므로 후보군에서 제외되고 가변인자 버전의 함수가 사용됨
    foo(3); // T
}

 

enable_if

  • c++표준에서 지원하는 도구

  • 1번째 인자가 true일 경우만 type이 정의됨

    • enable_if<true, int>::type  -> int

    • enable_if<true>::type -> void

    • enable_if<false, int>::type -> error

    • enable_if<false>::type -> error

#include <iostream>
using namespace std;

template<bool b, typename T = void> struct enable_if
{
};

template<typename T> struct enable_if<true, T>
{
    typedef T type;
};

int main()
{
    //enable_if<true, int>::type t0; // 타입이 int로 결정
    //enable_if<true>::type t1; // 타입이 void로 결정
    //enable_if<false, int>::type t2; // 타입이 없어서 error
}

 

enable_if 예제

  • 정수일때만 동작하는 함수를 만들고 싶을때

    • static_assert

      • 처리하게 되면 조건을 만족하지 않을 경우 컴파일 에러

    • enable_if

      • SFINAE 특성을 활용한 방법

      • 조건을 만족하지 않으면 함수를 생성하지 않고 다른 후보가 있다면 사용함

      • 확장성 유리

#include <iostream>
using namespace std;

// 정수일때만 함수 코드를 생성하고 싶음
template<typename T>
typename enable_if<is_integral<T>::value, int>::type
foo(T a)
{
    cout << "T" << endl;

    return 0;
}

void foo(...)
{
    cout << "not integer" << endl;
}

int main()
{
    foo(2.3);
}

 

enable_if 위치

  • 함수 리턴 타입

  • 함수 인자 타입 -> 생성자에서 주로 사용

  • 템플릿 인자 -> 함수 자체의 모양이 단순해 보이는 장점 있음

#include <iostream>
using namespace std;

// 함수 리턴 타입 위치
template<typename T>
typename enable_if<is_integral<T>::value, int>::type
foo(T a)
{
    cout << "T" << endl;

    return 0;
}

// 함수 인자 타입 위치(리턴 타입이 없는 생성자에서 주로 사용)
template<typename T>
void foo(T a, 
    typename enable_if<
    is_integral<T>::value>::type* = nullptr)
{
    cout << "T" << endl;
}

// 템플릿 인자(함수 자체의 원형이 최대한 유지됨으로 간결해 보이는 장점)
template<typename T, 
    typename enable_if<
    is_integral<T>::value>::type* = nullptr>
void foo(T a)
{
    cout << "T" << endl;
}

// 모두 만족하지 않을때 호출
void foo(...)
{
    cout << "not integer" << endl;
}

int main()
{
    foo(2); // 템플릿 함수 호출(현재 예제코드는 3개의 템플릿이 만족함으로 에러 발생함)
    foo(2.3); // foo(...) 호출
}

 

타입의 종류에 따라 다르게 동작하는 함수 생성 방법

  • type_traits(is_pointer) + if constexpr // C++17에서만 가능
  • type_traits + 함수 오버로딩(false_type, true_type)
    • 디스패치 함수 1개 더 필요
  • type_traits + enable_if
// C++17이상 가능 if constexpr 사용 방법

#include <iostream>
using namespace std;

template<typename T> void printv(const T& v)
{
    if constexpr(is_pointer<T>::value)
        cout << v << ":" << *v << endl;
    else
        cout << v << endl;
}

int main()
{
    int n = 0;
    printv(n);
    printv(&n);
}
// type_traits + 함수 오버로딩(false_type, true_type)

#include <iostream>
using namespace std;

template<typename T>
void printv_imp(const T& v, true_type)
{
    cout << v << ":" << *v << endl;
}

template<typename T>
void printv_imp(const T& v, false_type)
{
    cout << v << ":" << endl;
}

template<typename T> void printv(const T& v)
{
    printv_imp(v, is_pointer<T>());
}

int main()
{
    int n = 0;
    printv(n);
    printv(&n);
}
// type_traits + enable_if 사용 방법

#include <iostream>
using namespace std;

template<typename T> 
typename enable_if<is_pointer<T>::value>::type 
printv(const T& v)
{
    cout << v << ":" << *v << endl;
}

template<typename T> 
typename enable_if<!is_pointer<T>::value>::type
printv(const T& v)
{
    cout << v << ":" << endl;
}

int main()
{
    int n = 0;
    printv(n);
    printv(&n);
}
반응형

반응형

Policy Based Design

  • 클래스가 사용하는 정책을 템플릿 인자를 통해서 교체 할 수 있게 만드는 디자인

  • 성능 저하 없이 정책을 교체 할 수 있음

  • 대부분의 정책은 담은 "단위 전략 클래스"는 지켜야 하는 규칙이 있음

    • 규칙을 표현하는 코딩 방식은 없음(인터페이스 사용시 가상 함수이므로 약간의 오버헤드 발생), C++20 concept 문법

    • 우리가 만든 동기화 정책클래스는 "lock/unlock" 함수가 필요함

    • 템플릿 기반 라이브러리, 특히 STL에서 널리 사용되는 디자인 기법

#include <iostream>
using namespace std;

// 1개의 클래스로 정책 템플릿 인자를 통해서 다르게 구현 할 수 있도록 디자인
template<typename T, typename ThreadModel> class List
{
    ThreadModel tm;
public:
    void push_front(const T& a)
    {
        tm.lock();
        // 구현부 코드
        tm.unlock();
    }
};

// 싱글 쓰레드 기반 환경용
class NoLock
{
public:
    inline void Lock() {};
    inline void Unlock() {}
};

// 멀티 쓰레드 기반 환경용
class MutexLock
{
public:
    inline void Lock() {};
    inline void Unlock() {}
};

// 환경에 따라서 클래스 생성 시 전략을 선택 할 수 있음
// 싱글 쓰레드용 생성
//List<int, NoLock> s;
// 멀티 쓰레드용 생성
List<int, MutexLock> s;

int main()
{
    s.push_front(10);
}

 

STL allocator

  • C++에서 메모리 할당 방법은 다양함

    • new, malloc, calloc, win32 api, linux system call

  • STL에서는 각 데이터 컨테이너의 템플릿 인자로 메모리 할당 방식에 대한 전략을 선택할 수 있도록 지원함

#include <iostream>
using namespace std;

// STL의 Vector 생각
// 메모리 할당기
template<typename T> class allocator
{
public:
    T* allocate() {}
    void deallocate() {}
};

template<typename T, typename Ax = allocator<T>> class vector
{
    T* buff;
    Ax ax;
public:
    void resize(int n)
    {
        // 버퍼 재할당이 필요하다면 어떻게 할까요?
        // new, malloc, calloc, win32 api, linux system call
        T* p = ax.allocate(n);
        ax.deallocate(p);
    }
};

int main()
{
    vector<int, MyAlloc<int>> v(10);
    v.resize(20);
}

 

rebind

#include <iostream>
using namespace std;

template<typename T> class allocator
{
public:
    T* allocate(int sz) { return new T[sz]; }
    void deallocate(T* p) { delete[] p; }

    template<typename U> struct rebind
    {
        typename allocator<U> other;
    };
};

template<typename T, typename Ax = allocator<T> > class list
{
    struct NODE
    {
        T data;
        NODE* next, * prev;
    };
    //Ax ax; // allocator<int>
    //allocator<int>::rebind<NODE>::other ax; // allocator<NODE> ax;
    typename Ax::template rebind<NODE>::other ax; // allocator<NODE> ax;
public:
    void push_front(const T& a)
    {
        ax.allocate(1);
    }
};

int main()
{
    list<int> s;
    s.push_front(10);
}
반응형

반응형

CRTP 활용한 싱글톤(Singleton) 만들기

  • 싱글톤: 하나의 객체만 생성 할 수 있게 만드는 디자인 패턴
    • private 생성자
    • 복사와 대입 금지
    • 하나의 객체를 만들어서 리턴하는 static 멤버 함수

단일 Singletone 패턴 클래스

#include <iostream>
using namespace std;

class Cursor
{
private:
    Cursor() {}
public:
    Cursor(const Cursor& c) = delete;
    void operator=(const Cursor& c) = delete;

    static Cursor& getInstance()
    {
        static Cursor instance;
        return instance;
    }

};

int main()
{
    Cursor& c1 = Cursor::getInstance();
    Cursor& c2 = Cursor::getInstance();
}

CRTP 패턴 Singleton 패턴 클래스

#include <iostream>
using namespace std;

template<typename T>
class Singleton
{
protected:
    Singleton() {}
public:
    Singleton(const Singleton& c) = delete;
    void operator=(const Singleton& c) = delete;

    static T& getInstance()
    {
        static T instance;
        return instance;
    }
};

class Mouse : public Singleton<Mouse>
{
};

int main()
{
    Mouse& m1 = Mouse::getInstance();
    Mouse& m2 = Mouse::getInstance();
}

 

CRTP 활용한 Unique한 기반 클래스 만들기

  • 기반 클래스의 static memeber data는 모든 파생 클래스에 의해 공유됨

  • 파생 클래스 별로 다른 static member data가 필요한 경우, 서로 다른 기반 클래스를 사용해야 함

  • CRTP를 사용하면 모든 파생 클래스 별로 다른 타입의 기반 클래스를 만들 수 있음

static 멤버 데이터를 관리하는 단일 클래스

#include <iostream>
using namespace std;

class Object
{
public:
    static int cnt;

    Object() { ++cnt; }
    ~Object() { --cnt; }

    static int getCount() { return cnt; }
};
int Object::cnt = 0;

int main()
{
    Object c1, c2;
    cout << c1.getCount() << endl;
}

static 멤버 데이터를 관리하는 유일한 기반 클래스

#include <iostream>
using namespace std;

template<typename T>
class Object
{
public:
    static int cnt;

    Object() { ++cnt; }
    ~Object() { --cnt; }

    static int getCount() { return cnt; }
};
template<typename T> int Object<T>::cnt = 0;

class Mouse : public Object<Mouse>
{

};

class Keyboard : public Object<Keyboard>
{

};

int main()
{
    Mouse m1, m2;
    Keyboard k1, k2;
    cout << m1.getCount() << endl;
    cout << k1.getCount() << endl;
}
반응형

반응형

CRTP(Cruiously Recurring Template Pattern) 활용

  • 기반 클래스에서 파생 클래스의 이름을 사용할 수 있게 하는 기법

  • 파생 클래스를 만들때 기반 클래스의 템플릿 인자로 파생 클래스 이름을 전달

기준 코드

  • 메인 함수 호출 시 기반 클래스의 OnClick 함수가 호출됨
  • 파생 클래스의 OnClick 함수를 호출하기 위해서는?
#include <iostream>
using namespace std;

class Window
{
public:
    void msgLoop()
    {
        OnClick();
    }
    void OnClick() { cout << "Window OnClick" << endl; }
};

class FrameWindow : public Window
{
public:
    void OnClick() { cout << "FrameWindow OnClick" << endl; }
};

int main()
{
    FrameWindow fw;
    fw.msgLoop(); // OUTPUT : Window OnClick
}

 

구현 코드

  • 비 가상 함수(Non virtual function)를 가상 함수 처럼 동작하게 함
  • 파생 클래스를 만들때 기반 클래스의 템플릿 인자로 파생 클래스 이름을 전달
    • this 포인터를 파생 클래스 타입으로 캐스팅 후 함 수 호출
#include <iostream>
using namespace std;

template<typename T>
class Window
{
public:
    void msgLoop()
    {
        // this 포인터를 파생 클래스인 T 타입으로 캐스팅 후 호출    
        static_cast<T*>(this)->OnClick();
    }
    //가상함수로 만들면 가상함수 테이블이 관리됨
    void OnClick() { cout << "Window OnClick" << endl; }
};

class FrameWindow : public Window<FrameWindow>
{
public:
    void OnClick() { cout << "FrameWindow OnClick" << endl; }
};

int main()
{
    FrameWindow fw;
    fw.msgLoop(); // OUTPUT : FrameWindow OnClick
}
반응형

반응형

thin template

  • 템플릿의 단점 중 한가지는 코드 메모리의 증가
  • 모바일등 메모리가 부족한 환경에서 사용할 수 있는 기법

기준 코드

  • 4개의 함수 구성 * 3개의 타입 사용 = 총 12개의 함수 생성

#include <iostream>
using namespace std;

template<typename T> class Vector
{
    T* buff;
    int sz;
public:
    int size() const {}
    bool empty() const {}
    void push_front(const T& a) {}
    T& front() {}
};

int main()
{
    Vector<int> v1;
    Vector<short> v2;
    Vector<double> v3;
}

T를 사용하지 않는 모든 멤버를 기반클래스에 정의하고 상속받아서 구현

  • 2개의 함수 구성 * 3개의 타입 사용 + 기반 클래스 함수 2개 = 총 8개의 함수 생성

#include <iostream>
using namespace std;

class VectorBase
{
protected:
    int sz;
public:
    int size() const {}
    bool empty() const {}
};

template<typename T> class Vector : public VectorBase
{
    T* buff;
public:
    void push_front(const T& a) {}
    T& front() {}
};

int main()
{
    Vector<int> v1;
    Vector<short> v2;
    Vector<double> v3;
}

모든 멤버를 기반 클래스로 구현

  • 모든 T는 void*로 변경

  • void*를 바로 사용하면 캐스팅이 불편함으로 캐스팅을 책임지는 파생 클래스를 템플릿으로 제공

#include <iostream>
using namespace std;

class VectorBase
{
protected:
    void* buff;
    int sz;
public:
    int size() const {}
    bool empty() const {}
    void push_front(const void* a) {}
    void* front() {}
};

// void*를 바로 사용하면 캐스팅의 불편함이 있음
// 캐스팅을 책임지는 파생 클래스를 템플릿으로 제공
// 캐스팅만 책임지는 함수는 inline로 치환하면 함수화 되지 않음
template<typename T> class Vector : public VectorBase
{
public:
    inline void push_front(const T& a) { VectorBase::push_front(static_cast<void*>(a)) }
    inline T& front() { return static_cast<T&>(VectorBase::front()); }
};

int main()
{
    Vector<int> v1;
    Vector<short> v2;
    Vector<double> v3;
}
  • 기반 클래스 멤버를 직접 호출 방지를 위해 private 상속으로 변경

#include <iostream>
using namespace std;

class VectorBase
{
protected:
    void* buff;
    int sz;
public:
    int size() const {}
    bool empty() const {}
    void push_front(const void* a) {}
    void* front() {}
};

// void*를 바로 사용하면 캐스팅의 불편함이 있음
// 캐스팅을 책임지는 파생 클래스를 템플릿으로 제공
template<typename T> class Vector : private VectorBase
{
public:
    int size() const { return VectorBase::size(); }
    bool empty() const { return VectorBase::empty(); }
    inline void push_front(const T& a) { VectorBase::push_front(static_cast<void*>(a)); }
    inline T& front() { return static_cast<T&>(VectorBase::front()); }
};

int main()
{
    Vector<int> v1;
    Vector<short> v2;
    Vector<double> v3;
}
반응형

반응형

가변인자 템플릿(Variadic template)를 활용하여 tuple 전체 요소를 출력

 

#include <iostream>
#include <tuple>
using namespace std;

//튜플과 index_sequence를 받아서 튜플 전체 요소를 출력
template<typename TP, size_t ... I> 
void print_tuple_imp(const TP& tp, const index_sequence<I...>&)
{
    int x[] = { get<I>(tp)... };

    for (auto& n : x)
        cout << n << ", ";
}

template<typename TP>
void print_tuple(const TP& tp)
{
    // 튜플 사이즈를 구해서 index_sequence를 생성하여 print_tuple_imp에 전달
    print_tuple_imp(tp, make_index_sequence<tuple_size<TP>::value>());
}

int main()
{
    tuple<int, int, int> tp(1, 2, 3);

    print_tuple(tp);
}
반응형

반응형

기반 클래스의 멤버에 접근하는 방법(기반 지식)

  • 기반 클래스의 멤버와 파생클래스의 멤버의 이름이 동일할때는 자신(파생 클래스)의 멤버가 우선

  • 기반 클래스의 멤버에 접근하는 방법

    • d.Base::value

    • static_cast<Base&>(d).value;

  • 값 캐스팅과 참조 캐스팅

    • static_cast<Base>(d): 임시객체 생성. lvalue가 될 수 없음

    • static_cast<Base&>(d): 임시객체를 생성 안함. lvalue가 될 수 있음

#include <iostream>
using namespace std;

struct Base
{
    int value = 10;
};

struct Derived : public Base
{
    int value = 20;
};

int main()
{
    Derived d;

    cout << d.value << endl; // 20
    cout << d.Base::value << endl; // 10

    cout << static_cast<Base>(d).value << endl; // 10 임시객체 생성
    cout << static_cast<Base&>(d).value << endl; // 10 참조

    //static_cast<Base>(d).value = 30; // Error
    static_cast<Base&>(d).value = 30; // OK

}

 

Tuple_element

  •  tuple이 가진 N번째 요소의 타입을 구하는 템플릿

  • 메인 템플릿(Main template) 생성, 구현은 불필요함으로 생략

  • 0번째 요소를 구하는 부분 특수화 구현

  • N번째 요소를 구하는 부분 특수화 구현(Recursive)

#include <iostream>
using namespace std;

// tuple 메인 템플릿
template<typename ... Types> struct xtuple
{
    static constexpr int N = 0;
};

// tuple 부분 특수화
template<typename T, typename ... Types> 
struct xtuple<T, Types...> : public xtuple<Types...> // 타입을 상속을 통해 재귀 저장 효과
{
    T value;
    xtuple() = default; // 기본 생성자
    xtuple(const T& v, const Types& ... args) 
        : value(v), xtuple<Types...>(args...) {} // Pack expansion으로 가변인자 전달

    static constexpr int N = xtuple<Types...>::N + 1;
};

// 메인 템플릿
template<size_t N, typename TP> struct xtuple_element;

// 요소의 타입을 구할 수 있도록 부분 특수화
template<typename T, typename... Types> struct xtuple_element<0, xtuple<T, Types...>>
{
    typedef T type;
    typedef xtuple<T, Types...> tupleType;
};

// 요소의 타입을 구할 수 있도록 부분 특수화
template<size_t N, typename T, typename... Types> 
struct xtuple_element<N, xtuple<T, Types...>>
{
    typedef typename xtuple_element<N-1, xtuple<Types...>>::type type;
    typedef typename xtuple_element<N-1, xtuple<Types...>>::tupleType tupleType;
};

// xtuple 요소를 꺼내는 함수
template<size_t N, typename TP>
typename xtuple_element<N, TP>::type& xget(TP& tp)
{
    return static_cast<typename xtuple_element<N, TP>::tupleType&>(tp).value;
}

int main()
{
    xtuple<int, double, char> t3(1, 3.4, 'A'); // 1
    
    cout << xget<1>(t3) << endl; // 3.4
    xget<1>(t3) = 1.1;
    cout << xget<1>(t3) << endl; // 1.1
}
반응형

반응형

tuple C++11 표준

  • 서로 다른 타입의 객체를 N개 보관하는 템플릿
#include <iostream>
#include <tuple>
using namespace std;

int main()
{
    tuple<> t0;
    tuple<int> t1(1);
    tuple<int, double, int, char> t4(1, 3.4, 2, 'A');

    get<2>(t4) = 15; // 참조 리턴으로 값 변경 가능

    cout << get<2>(t4) << endl; // 2
}

tuple 만들기

  • 가변인자 템플릿을 사용해서 메인 템플릿(Main template)을 만듬

  • 1개의 요소를 보관할 수 있도록 부분 특수화 구현

  • 상속을 사용해서 N개를 보관할 수 있게 구현

  • 생성자등 필요한 멤버 추가

#include <iostream>
using namespace std;

// tuple 메인 템플릿
template<typename ... Types> struct xtuple
{
    static constexpr int N = 0;
};

// tuple 부분 특수화
template<typename T, typename ... Types> 
struct xtuple<T, Types...> : public xtuple<Types...> // 타입을 상속을 통해 재귀 저장 효과
{
    T value;
    xtuple() = default; // 기본 생성자
    xtuple(const T& v, const Types& ... args) 
        : value(v), xtuple<Types...>(args...) {} // Pack expansion으로 가변인자 전달

    static constexpr int N = xtuple<Types...>::N + 1;
};

int main()
{
    xtuple<> t0;
    xtuple<int> t1(3);
    xtuple<int, double, char> t4(1, 3.4, 'A');
}
반응형

+ Recent posts