【发布时间】:2012-04-05 00:07:22
【问题描述】:
我正在尝试使用 g++-4.7 (20120228-1) 编译以下程序:
#include <cstdlib>
#include <tuple>
template<typename X> struct Y {};
template<typename T, size_t Level, size_t TermLevel> struct A;
// (B) dummy for T=tuple<int, Ts...> just to show it works for simple expansions
template<typename ... Ts, size_t Level, size_t TermLevel>
struct A<std::tuple<int, Ts...>, Level, TermLevel>
{
A<std::tuple<int, Ts...>, Level+1, TermLevel> value;
};
template<typename ... Ts, size_t Level>
struct A<std::tuple<int, Ts...>, Level, Level> {};
// (C) ambiguous partial specialization
template<typename ... Ts, size_t Level, size_t TermLevel>
struct A<std::tuple<Y<Ts>...>, Level, TermLevel>
{
A<std::tuple<Y<Ts>...>, Level+1, TermLevel> value;
};
template<typename ... Ts, size_t Level>
struct A<std::tuple<Y<Ts>...>, Level, Level> {};
int main(int argc, const char *argv[])
{
A<std::tuple<int, float, int>, 0, 5> tint;
A<std::tuple<Y<int>, Y<float>>, 0, 1> tn;
return 0;
}
导致歧义如下:
g++-4.7 -g -O0 -std=c++0x specialization_orig.cc -o specialization_orig
specialization_orig.cc: In instantiation of 'struct A<std::tuple<Y<int>, Y<float> >, 0ul, 1ul>':
specialization_orig.cc:33:43: required from here
specialization_orig.cc:23:49: error: ambiguous class template instantiation for 'struct A<std::tuple<Y<int>, Y<float> >, 1ul, 1ul>'
specialization_orig.cc:21:8: error: candidates are: struct A<std::tuple<Y<Ts>...>, Level, TermLevel>
specialization_orig.cc:27:8: error: struct A<std::tuple<Y<Ts>...>, Level, Level>
specialization_orig.cc:23:49: error: 'A<std::tuple<Y<Ts>...>, Level, TermLevel>::value' has incomplete type
specialization_orig.cc:6:61: error: declaration of 'struct A<std::tuple<Y<int>, Y<float> >, 1ul, 1ul>'
这有点奇怪,因为可变参数扩展适用于参数包的简单扩展,但一旦可变参数包扩展为嵌套在其他模板类型中,就会失败。
这仅仅是编译器的疯狂还是我做错了什么?
【问题讨论】:
-
ICC 也拒绝此代码,并出现“错误:多个部分特化匹配类的模板参数列表”A<:tuple>, Y
>, 1UL , 1UL>" "A<:tuple>...>, Level, TermLevel>" "A<:tuple>...>, Level, Level>" A...>, Level+1, TermLevel> value;”,但 CLang 接受它。任何有 Comeau 或足够新的 VC++ 的人都可以试试这个,因为我无法访问 VS2013,而且 Comeau 的试用已经失败?
标签: c++ templates c++11 template-specialization