std::priority_queue
의 두 번째 템플릿 매개 변수에 대한 특정 솔루션에 관심이있는 경우 typename std::priority_queue<T>::container_type
을 어떤 방식 으로든 사용할 수 있다고 가정합니다 (예를 들어, nwp; +1의 솔루션 참조).
보다 일반적인 솔루션에 관심이 만약 (비에만 두 번째 기본 템플릿 유형 만 std::priority_queue
비), 난 당신이 먼저 목록에서 n 번째 유형을 estract하는 유형 특성 type_n
을 개발할 수 있다고 생각
다음
template <std::size_t N, typename T0, typename ... Ts>
struct type_n
{ using type = typename type_n<N-1U, Ts...>::type; };
template <typename T0, typename ... Ts>
struct type_n<0U, T0, Ts...>
{ using type = T0; };
유형의 특성 type_cnt_n
(즉, 사용 type_n
)
template <std::size_t, typename>
struct type_cnt_n;
template <std::size_t N, template <typename ...> class Cnt, typename ... Ts>
struct type_cnt_n<N, Cnt<Ts...>>
{ using type = typename type_n<N, Ts...>::type; };
과 마지막 (NWP의 대답은 다음과 같은) 템플릿 템플릿 매개 변수의 n 번째 형식 인수를 추출 이 솔루션의 make_priority_queue()
기능
template <typename T, typename Cmp>
auto make_priority_queue (Cmp const & cmp)
{ return std::priority_queue<T,
typename type_cnt_n<1U, std::priority_queue<T>>::type, Cmp> { cmp }; }
문제는 유일한 유형 전용 템플릿 매개 변수 템플릿 템플릿 유형 작동 (그래서 std::map
으로, std::vector
으로, std::priority_queue
로 작동하지만 std::array
작동하지 않습니다) 즉.
다음은 전체 작업 ... 음, 전체 컴파일 ... 예를 직접
#include <queue>
#include <iostream>
template <std::size_t N, typename T0, typename ... Ts>
struct type_n
{ using type = typename type_n<N-1U, Ts...>::type; };
template <typename T0, typename ... Ts>
struct type_n<0U, T0, Ts...>
{ using type = T0; };
template <std::size_t, typename>
struct type_cnt_n;
template <std::size_t N, template <typename ...> class Cnt, typename ... Ts>
struct type_cnt_n<N, Cnt<Ts...>>
{ using type = typename type_n<N, Ts...>::type; };
template <typename T, typename Cmp>
auto make_priority_queue (Cmp const & cmp)
{ return std::priority_queue<T,
typename type_cnt_n<1U, std::priority_queue<T>>::type, Cmp> { cmp }; }
int main()
{
auto cmpFn = [](std::string const & l, std::string const &r)
{ return r < l; };
auto pq = make_priority_queue<std::string>(cmpFn);
}
하지,하지만 확실히 않을 것, 가능한 것 같다 템플릿 별명? – WhozCraig