2016-09-30 2 views
1

여러 저장된 클래스에 대한 일반 캐시가 필요한 프로젝트에서 작업 중입니다. 내 기본 클래스는 다음과 같습니다Variadic 템플릿 클래스 - 가변 멤버 함수

template<typename ... Types> 
class VarCache{ 
    template<typename T> 
    using item_type = shared_ptr<holder<T>>; 
    template<typename T> 
    using map_type = map<int64_t,item_type <T>>; 
public: 
    std::tuple<map_type<Types>...> caches; 
}; 

내가 인수를 허용하지 않을 것이다 함수를 작성해야하지만 캐시 개체에 호출함으로써, 반복적으로 저장된 모든지도의 변종을 통과, 행동 (불필요한 항목을 제거하는) perfrom 것 .

예 :

I VarCache<A,B,C> cacheTcache.prune_all_variants();를 호출하여

A,B,C 중 하나입니다 방법 prune<T>(); 내가 캐시

prune<A>(); 
prune<B>(); 
prune<C>(); 

수행 할이 수 있나요?

답변

2

매개 변수 팩의 각 요소에 대해 함수를 호출하는 데 사용할 수있는 몇 가지 트릭이 있습니다. 여기에 하나 개의 옵션이다 : C++ 17에서

void prune_all_variants() { 
    (void)std::initializer_list<int> { 
     (prune<Types>(), 0)... 
    }; 
} 

이 다음 fold expressions을 사용하여 단순화 할 수 있습니다

void prune_all_variants() { 
    (prune<Types>(), ...); 
} 
+0

고맙습니다. 큰 :). – semtexzv

1

당신은 사용할 수 있습니다 :

void prune_all_variants() 
{ 
    int dummy[] = {0, (prune<Types>(), void(), 0)...}; 
    static_cast<void>(dummy); // avoid warning for unused variable. 
} 

또는 배 표현식 C++ 17 :

void prune_all_variants() { 
    (static_cast<void>(prune<Types>()), ...); 
} 
관련 문제