使用依赖类型的模板
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();
}
当模板位于模板类型参数中时,如何使用模板参数调用模板:
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
.
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();
}