당신은 template template parameter (유형의 번호와 템플릿을 위해 일해야 인수)를 사용하여, 그런 일을 할 수있는 :
template <typename T>
using Template = typename get_template<SomeType>::type<T>;
로 :
다음
template <typename T>
struct get_template;
template <template <class...> class Y, typename... Args>
struct get_template<Y<Args...>> {
template <typename... Others>
using type = Y<Others...>;
};
템플릿을 얻기 위해 @Yakk에 의해 주석에 언급 된 위의 경우에만 형식 인수가있는 서식 파일에 대해서만 작동합니다. 당신은, 유형 및 비 타입 인수의 특정 패턴 템플릿 전문 수 있습니다 예컨대 :
// Note: You need the first size_t to avoid ambiguity with the first specialization
template <template <class, size_t, size_t...> class Y, typename A, size_t... Sizes>
struct get_template<Y<A, Sizes...>> {
template <class U, size_t... OSizes>
using type = Y<U, OSizes...>;
};
...하지만 당신은 임의의 템플릿을 전문으로 할 수 없습니다.
DEMO (Foo
및 std::pair
포함) :
#include <type_traits>
#include <map>
struct Bar{};
template <typename T>
struct Foo {};
using SomeType = Foo<Bar>;
template <typename T>
struct get_template;
template <template <class...> class Y, typename... Args>
struct get_template<Y<Args...>> {
template <typename... Others>
using type = Y<Others...>;
};
template <typename T>
using Template = typename get_template<SomeType>::type<T>;
static_assert(std::is_same<SomeType, Template<Bar>>::value, "");
static_assert(std::is_same<Foo<int>, Template<int>>::value, "");
using PairIntInt = std::pair<int, int>;
using PairIntDouble = std::pair<int, double>;
template <typename U1, typename U2>
using HopeItIsPair =
typename get_template<PairIntDouble>::type<U1, U2>;
static_assert(std::is_same<PairIntDouble, HopeItIsPair<int, double>>::value, "");
static_assert(std::is_same<PairIntInt, HopeItIsPair<int, int>>::value, "");
가능한 [부모 유형 가져 오기] (http://stackoverflow.com/questions/31429363/get-the-type-of-a-parent) –
가능한 예는 약간의 인위적인 내용입니다. 별칭을 사용합니다. "SomeType"은 이미 "Foo"(static_assert (std :: is_same > :: 값, ");)과 동일합니다. 아마도'Foo' 부분 만 복구하려고 할 것입니다. 다른 유형으로 인스턴스화하고 싶기 때문입니다. –
AndyG
@AndyG 예 결국 다른 유형으로 인스턴스화하고 싶습니다. 질문을 편집했습니다. –