반응형
템플릿 클래스를 typedef하는 방법은 무엇입니까?
이 질문에 이미 답변이 있습니다.
- C ++ 템플릿 typedef 1 답변
어떻게해야 합니까? 다음과 같은 것 :typedef
template 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
반응형
'ProgramingTip' 카테고리의 다른 글
메모리에 테이블 형식 데이터를 유지하기위한 데이터 구조? (0) | 2020.10.29 |
---|---|
Python에 개체 고유 식별자가 있습니까? (0) | 2020.10.29 |
srand () — 한 번만 호출하는 이유는 무엇입니까? (0) | 2020.10.29 |
모델과 관련이없는 rails simple_form 필드 (0) | 2020.10.29 |
동일한 키를 가진 개체가 ObjectStateManager에 이미 있습니다. (0) | 2020.10.29 |