[英]Use template from dependent type
當模板位於模板類型參數中時,如何使用模板參數調用模板:
template<template<class> class Template, class T>
struct MakeType
{
using type = Template<T>;
};
template<class Policy>
struct Blah
{
using type = MakeType< /*A non-instantiated template member in Policy*/, int>::type;
};
例如, Policy
可以有一個模板成員ArrayType
:
class ExamplePolicy
{
template<class T>
using ArrayType = std::vector<T>;
};
如何使用模板Policy::ArrayType
作為Template
從 Blah 調用MakeType
。
直接調用MakeType
效果很好:
static_assert(std::is_same_v<std::vector<int>, MakeType<std::vector, int>::type>);
會編譯
這能解決你的問題嗎?
// -*- compile-command: "g++ SO.cpp"; -*-
#include <vector>
template <template <class> class Template, class T>
struct MakeType
{
using type = Template<T>;
};
template <class Policy>
struct Blah
{
using type = typename MakeType<Policy::template ArrayType, int>::type;
type foo()
{
return type();
}
};
struct ExamplePolicy
{
template <class T>
using ArrayType = std::vector<T>;
};
int main()
{
Blah<ExamplePolicy> blah;
auto tmp = blah.foo();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.