成员模板的别名模板

alias template for member template

假设我有一个模板:

template<typename T>
struct Outer {
    template<typename T1>
    struct Inner {

    };
};

我想要一个别名模板Alias:

template<typename T>
using Alias = Outer<T>::template Inner; // this won't work

using IntOuter = Alias<int>;

因此 IntOuter<double>Outer<int>::template Inner<double> 相同。你如何定义Alias?或者有可能吗?

编辑:

我希望能够即时创建 SomeOuter 以便模板 foo:

template<template<typename> class>
struct Foo {
};

Foo<Alias<int>> 等同于 Foo<Outer<int>::template Inner>

或者做这样的事情:

template<typename T>
using SomeFoo = Foo<Alias<T>>;

你可以

template<typename T, typename T1>
using Alias = typename Outer<T>::template Inner<T1>;

template<typename T1>
using IntOuter = Alias<int, T1>;

或直接

template<typename T1>
using IntOuter = Outer<int>::Inner<T1>;

然后 IntOuter<double> 你会得到 Outer<int>::Inner<double>.

LIVE

你做不到你要求的。

这就是您问题的答案。你问的问题太狭隘了,没有足够的背景,那是我能做的最好的。


如果你想在 C++ 中进行真正的元编程,你可以将模板转换为类型或值。有关一种方法,请参阅 boost hana 库。

但是none会与

互动
template<template<typename> class>
struct Foo {
};

"Out of the box".

template<class T>struct tag_t{using type=T;};
template<class T>constexpr tag_t<T> tag{};
template<template<class...>class Z>struct ztemplate_t{
  template<class...Ts>using apply=Z<Ts...>;
  template<class...Ts>
  constexpr tag_t<Z<Ts...>> operator()(tag_t<Ts>...)const{return {};}
};
template<template<class...>class Z>constexpr ztemplate_t<Z> ztemplate{};
template<class Tag>using type_t=typename Tag::type;
#define TYPE(...) type_t<decltype(__VA_ARGS__)>

现在

template<typename T>
constexpr auto Alias = ztemplate<Outer<T>::template Inner>;

现在是一个类似于模板的值。

映射 Foo 到:

template<template<class...>class Z>
constexpr tag_t<Foo<Z>> foo( ztemplate_t<Z> ){return {};}

让你做 TYPE( foo(Alias( tag<int> ) ) ).

这可能不是你想要的。