我試圖編譯克++下面的程序 - 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 <性病::元組,Y >,1UL,1UL>「 」 A <性病::元組 ...>,水準儀,TermLevel>」 「A <性病::元組 ...>,級別,級別>」 A <性病::元組 ...> Level + 1,TermLevel> value;「,但CLang接受它。任何人只要有科莫或新的足夠VC++可以試試這個,因爲我沒有獲得VS2013和科莫嘗試,它出的是跌? –
LThode
2014-11-12 20:15:09