C++ 유틸
C++ Traits
코딩조무사
2017. 10. 3. 23:17
스칼라의 Traits C++에서 비슷하게 구현하기
operator()로 구현한다고 했을 때
1. 클래스 기반 구현
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | struct Traits{ inline void operator()(){} }; template<typename _Base = Traits> struct WorkingA : _Base{ void operator()(){ _Base::operator()(); printf("workingA\n"); } }; template<typename _Base = Traits> struct WorkingB : _Base{ void operator()(){ _Base::operator()(); printf("workingB\n"); } }; |
1 2 3 4 5 6 7 8 9 10 11 12 | int main(int argc, const char * argv[]){ WorkingA<> a; //단독 WorkingB<> b; //단독2 WorkingA<WorkingB<>> ab; //AB WorkingB<WorkingA<>> ba; //BA a(); b(); ab(); ba(); } | cs |
2. operator() 좀 이상한것 같기도함
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | struct Traits{ inline void operator()(){} }; struct WorkingC{ template<typename _Base = Traits> WorkingC operator()(_Base&& b = _Base()){ printf("workingC\n"); return *this; } } workingC; struct WorkingD { template<typename _Base = Traits> WorkingD operator()(_Base&& b = _Base()){ printf("workingD\n"); return *this; } }workingD; | cs |
2-1 operator() 테스트
1 2 3 4 | int main(int argc, const char * argv[]){ workingD(workingC(workingC(workingD()))); } | cs |