C++ 通过重用模板参数创建类型

C++ 通过重用模板参数创建类型,c++,templates,alias,reusability,C++,Templates,Alias,Reusability,我使用模板参数,并希望基于在其他位置定义的模板参数构造新类型。这有时是可行的,但我想我从根本上,从概念上错过了一些东西 在下面的最小(我希望不要太扭曲)示例中,我希望example和MakeAnotherX::type的类型是相同的,但它们并不总是相同的 struct Something {}; template <template <typename> typename TemplateParameter> struct Example { template &

我使用模板参数,并希望基于在其他位置定义的模板参数构造新类型。这有时是可行的,但我想我从根本上,从概念上错过了一些东西

在下面的最小(我希望不要太扭曲)示例中,我希望
example
MakeAnotherX::type
的类型是相同的,但它们并不总是相同的

struct Something {};

template <template <typename> typename TemplateParameter>
struct Example {

  template <typename T>
  using InputTemplate = TemplateParameter<T>; 

  using FullType = TemplateParameter<Something>;

 private:
  FullType fData;
};

template <typename T>
struct GenericTemplate {};

// this works with (recent) GCC, but the static_assert below fails with clang
template <typename ExistingType, 
          template <typename> typename _InputTemplate 
                                = ExistingType::template InputTemplate> 
struct MakeAnother1 {
  using type = Example<_InputTemplate>;
};

// this compiles, but static_assert below fails for both (recent) gcc and clang
template <typename ExistingType> 
struct MakeAnother2 {
  template <typename T> using _InputTemplate 
                     = typename ExistingTemplate::template InputTemplate<T>;
  using type = Example<_InputTemplate>;
};


#include <type_traits>
int main() {
  using type1 = Example<GenericTemplate>;
  static_assert(std::is_same<type1, MakeAnother1<type1>::type >::value, "this is not the same");
  static_assert(std::is_same<type1, MakeAnother2<type1>::type >::value, "this is not the same");

 return 0;
}
struct Something{};
模板
结构示例{
模板
使用InputTemplate=TemplateParameter;
使用FullType=TemplateParameter;
私人:
全型fData;
};
模板
结构GenericTemplate{};
//这适用于(最近的)GCC,但是下面的静态断言在使用clang时失败
模板
结构MakeAnother1{
使用类型=示例;
};
//这可以编译,但是下面的静态_断言对于(最近的)gcc和clang都失败
模板
结构MakeAnother2{
使用输入模板的模板
=typename ExistingTemplate::template InputTemplate;
使用类型=示例;
};
#包括
int main(){
使用type1=示例;
静态断言(std::is_same::value,“这不一样”);
静态断言(std::is_same::value,“这不一样”);
返回0;
}

到目前为止,我正在使用gcc和clang(-std=c++17)进行测试(和生产),但一般来说,我只希望有符合标准的代码。我真的希望能够在不同的上下文和类中重用模板参数,如上面的最小示例所述

以稳健的方式实现这一目标的正确方法是什么?另外:MakeAnother1与GCC一起工作这一事实是GCC的一个特性还是一个缺陷(对于clang,反之亦然)

“真正的代码”要复杂得多,所以我特别希望那些“Make”helper结构能够处理新类型

更新 早在6年前,人们就提出了这个问题的一个概念上相同的版本。消息是,今天这项工作在gcc版本5和更高版本上进行,但似乎没有其他编译器支持它。不幸的是,关于这一点的讨论似乎停滞不前,提醒一下会有什么好处


在这个阶段:在这个更新之后,如果你愿意,我们当然可以现在关闭这个报告。谢谢你的帮助

请使用描述性名称。在做一些简单的事情时,对模板类型使用
T
是可以的;一旦你有了多种不同类型的类型和多个浮动的模板,这将毫无意义地令人困惑。我很乐意改变这一点,但要澄清的是,你希望我使用更长、唯一的类和模板参数名称?有意义的名称。不是a
b
c
任何地方。说出这件事的意思。命名很难,但未命名的东西读起来很痛苦。让我们看看这是否更好…这是