嵌套可变参数模板:gcc 或 clang 中的错误?
以下代码无法使用 gcc 4.7 (20120114) 编译,但可以使用 clang++ 3.0 编译良好。这是 gcc、clang 中的错误还是只是因为我尝试做的事情在 c++11 中不允许?
template< typename... args >
struct A {
template< typename head, typename... tags >
struct Inner : public Inner<tags...> {
};
template< typename head >
struct Inner<head> {
// assume both args... and tags... must be used to
// calculate TYPE
typedef int TYPE;
};
};
template< typename... args >
struct B : A<args...> {
template<typename... tags>
typename A<args...>::template Inner<tags...>::TYPE x() {
return 0;
}
};
int main(int argc, const char *argv[]) {
B<int, int, int> b;
b.x<char, short, long, double>();
return 0;
}
上面的代码是我尝试做的一个非常简单的示例,但本质是我需要 args... 类型和 tag... 类型来计算函数的返回类型。这怎么能做到呢?
The following code does not compile with gcc 4.7 (20120114) , but compiles fine with clang++ 3.0. Is this a bug in gcc, clang or just because what I try to do is not allowed in c++11?
template< typename... args >
struct A {
template< typename head, typename... tags >
struct Inner : public Inner<tags...> {
};
template< typename head >
struct Inner<head> {
// assume both args... and tags... must be used to
// calculate TYPE
typedef int TYPE;
};
};
template< typename... args >
struct B : A<args...> {
template<typename... tags>
typename A<args...>::template Inner<tags...>::TYPE x() {
return 0;
}
};
int main(int argc, const char *argv[]) {
B<int, int, int> b;
b.x<char, short, long, double>();
return 0;
}
The code above is a very simplified example of what I try to do, but the essence is that I need both the args... types and the tags... types to calculate the return type of the function. How can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是否是 gcc 的错误,但使其在 gcc 上编译的标准解决方案是声明空可变参数版本,然后对其进行专门化:
演示: http://ideone.com/MFKVY
Not sure if it's a bug of gcc or not, but the standard solution to make it compile on gcc is to declare the empty variadic version and then specialize it:
Demo: http://ideone.com/MFKVY