【发布时间】:2012-03-20 07:25:36
【问题描述】:
我有一些对我来说似乎很明确的代码,但 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 有什么问题吗?当我写它时,它似乎很合理:)
【问题讨论】:
-
与您的问题无关,但这意味着
InnerCombiner<Mix<A, B>::types>::type,或者InnerCombiner特化意味着匹配Mix<MixedMixins...>或通用模板模板参数而不是std::tuple。跨度> -
我认为这是正确的,因为 T 旨在成为某种 Mix<...>,并且 mix 具有 ::types,因此未专门化的 InnerCombiner 提取类型以应用于部分专业化的 InnerCombiner。那会奏效吗?不过你是对的,匹配 Mix<...> 会更优雅。我想我仍然会遇到同样的问题。
-
PS 253 答案,没有问题?!你从来不被难倒吗? :)
-
是的,这行得通。当程序中没有任何成员时,发现一个
int成员的 GCC 错误消息让我难过(说到)。 -
@LucDanton 是的,我也注意到了。我认为这可能是编译器在出错后尝试继续的策略,这样它可以提供更多错误,您可以一次修复它们。
标签: c++ templates c++11 variadic-templates template-templates