ProgramingTip

템플릿 클래스를 typedef하는 방법은 무엇입니까?

bestdevel 2020. 10. 29. 08:24
반응형

템플릿 클래스를 typedef하는 방법은 무엇입니까?


이 질문에 이미 답변이 있습니다.

어떻게해야 합니까? 다음과 같은 것 :typedeftemplate class

typedef std::vector myVector;  // <--- compiler error

두 가지 방법을 알고 있습니다.

(1) #define myVector std::vector // not so good
(2) template<typename T>
    struct myVector { typedef std::vector<T> type; }; // verbose

C ++ 0x에 더 나은 것이 있습니까?


예. "라고하는 템플릿 "이라고하며 C ++ 11의 새로운 기능입니다.

template<typename T>
using MyVector = std::vector<T, MyCustomAllocator<T>>;

그러면 예상대로 정확하게 사용됩니다.

MyVector<int> x; // same as: std::vector<int, MyCustomAllocator<int>>

GCC는 4.7부터 지원하고 Clang은 3.0부터 지원하고 MSVC는 2013 SP4에 지원했습니다.


C ++ 03에서는 상속 상속 (공개 또는 비공개) 할 수 있습니다.

template <typename T>
class MyVector : public std::vector<T, MyCustomAllocator<T> > {};

좀 더 많은 작업 (복사 복사 생성자, 할당 연산자)이 필요하지만 꽤 가능합니다.

참고 URL : https://stackoverflow.com/questions/6907194/how-to-typedef-a-template-class

반응형