簡體   English   中英

成員模板的別名模板

[英]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>

生活

你不能做你所要求的。

那就是你問題的答案。 您問一個沒有足夠背景知識的狹窄問題,這是我所能做的最好的。


如果要使用C ++進行認真的元編程,則可以將模板傳輸為類型或值。 有關一種方法,請參見boost hana庫。

但是沒有人會與

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

“盒子外面”。

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> ) ) )

這可能不是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM