c++ - 如何访问可变模板参数包成员中存在的内部模板 typedef?

标签 c++ templates c++11 variadic-templates template-templates

我有一些代码对我来说似乎没有歧义,但 gcc4.7 令人窒息:

#include <iostream>
#include <tuple>

using namespace std;

// Container for mixins
template<template<typename> class... Mixins>
struct Mix : Mixins<Mix<Mixins...>>... {
  typedef tuple<Mixins<Mix<Mixins...>>...> types;
};

// Outer layer extracts the type tuple from the argument
template<typename T>
struct InnerCombiner {
  typedef typename InnerCombiner<typename T::types>::type type;
};

// Typedef type to be a new mix of the inner mixins of the MixedMixins
template<typename... MixedMixins>
struct InnerCombiner<tuple<MixedMixins...>> {
  // This line is the problem. The compiler doesn't seem to be able to make sense
  // of the reference to the inner mixin template template classes
  typedef Mix<MixedMixins::InnerMixin...> type;
};

template<typename Mixed>
struct A {
  template<typename MixedInner>
  struct InnerMixin {
    void foo() { cout << "foo() loves you!" << endl; };
  };
};

template<typename Mixed>
struct B {
  template<typename MixedInner>
  struct InnerMixin {
    void bar() { cout << "bar() loves you!" << endl; };
  };
};

// I'm going to write out the type I expect ic to have. Oh god, it's so nasty:
// Mix<
//   A<Mix<A,B>>::InnerMixin<Mix<A<Mix<A,B>>::InnerMixin,B<Mix<A,B>>::InnerMixin>,
//   B<Mix<A,B>>::InnerMixin<Mix<A<Mix<A,B>>::InnerMixin,B<Mix<A,B>>::InnerMixin>
// >


int main() {
  InnerCombiner<Mix<A,B>>::type ic;

  ic.bar(); // Not working.
}

这样访问 InnerMixins 有问题吗?写的时候觉得挺有道理的:)

最佳答案

我可以通过指定 InnerMixin 模板使其在 clang 3.0 上运行:

typedef Mix<MixedMixins::template InnerMixin...> type;
//                       ^^^^^^^^

但它在 g++ 4.8 上仍然失败,

3.cpp:23:52: error: parameter packs not expanded with ‘...’:
3.cpp:23:52: note:         ‘MixedMixins’

关于c++ - 如何访问可变模板参数包成员中存在的内部模板 typedef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9782803/

相关文章:

c++ - 如何使用opencv检测图像中的文本样式?

templates - ArrayData 构造函数的参数必须是对象或关联数组

c++ - 为什么编译器需要复制构造函数,需要并拥有移动构造函数而不使用它们中的任何一个?

c++ - 关于分配器重新绑定(bind)的查询

C++做引用占用内存

c++ - 在没有 DirectX 的情况下在 Windows 上获得精确的屏幕刷新率?

c++ - 如何向模板类声明模板友元函数?

c++ - 指向另一个函数的函数参数

c++ - 在集合上运行的函数模板,它有一个默认的 "transform"函数,什么都不做

c++ - 范围 For 循环帮助 : Expected initializer before ":" token