这个模板语法是非法的吗?
我得到一个“内部编译器错误”与此使用GCC 4.9.2:
#include <type_traits> template <typename T, typename, int, template <typename U, U, U> class> struct Sort; template <typename T, template <T...> class Z, TN, T... Is, template <typename U, U, U> class Comparator> struct Sort<T, Z<N, Is...>, 0, Comparator> { template <T I> struct less_than : std::integral_constant<bool, Comparator<T, I, N>::value> { }; }; int main() {} 错误消息指出:
c:\ ADandD> g ++ -std = c ++ 14 ComparatorAndSorterTGeneralized.cpp ComparatorAndSorterTGeneralized.cpp:254:80:内部编译器错误:在tsubst中,在cp / pt.c:11738
template<T I> struct less_than : std::integral_constant<bool, Comparator<T,I,N>::value> {}; ^请提交一份完整的错误报告,如果适用,请预处理源代码。 有关说明,请参阅http://gcc.gnu.org/bugs.html 。
 问题是正在使用的模板<typename U, U, U> class Comparator 。 我从来没有试过这个。 起初,我尝试了模板<typename T, T, T> class Comparator ,但由于模板阴影不能编译,所以我知道这是非法的。 然后将它改为U仍然没有编译,所以我认为整个想法是不允许的。 
更新:实例化时,这在Visual Studio 2015 Preview中编译:
 #include <type_traits> template <typename T, typename, int, template <typename U, U, U> class> struct Sort; template <typename T, template <T...> class Z, TN, T... Is, template <typename U, U, U> class Comparator> struct Sort<T, Z<N, Is...>, 0, Comparator> { template <T I> struct less_than : std::integral_constant<bool, Comparator<T, I, N>::value> { }; }; template <int...> struct index_sequence {}; template <typename T, TA, T B> struct LessThan : std::integral_constant < bool, A<B> {}; enum { QuickSort, MergeSort, InsertionSort }; int main() { Sort<int, index_sequence<4, 5, 6, 1, 2, 7>, QuickSort, LessThan> quickSort; } 
	
 template <typename T, typename, int, template <typename U, U, U> class> struct Sort; 
这是完全合法的。
它可以像这样重新声明,给所有的参数命名:
 template <typename T, typename T2, int I, template <typename U, UX, U Y> class TT> struct Sort; 
 它声明了一个类模板Sort ,它有四个模板参数,types参数T ,第二个types参数T2 (原始中未命名),一个非types模板参数I和一个模板模板参数TT 。 
 模板模板参数TT必须是一个带有三个模板参数的类模板, U是types参数,第二个和第三个( X和Y )是Utypes的非types模板参数。 
  Sort的第四个模板参数的一个合适的参数可能是这样的: 
 template <typename T, T t1, T t2> class Foo { static const bool value = t1 < t2; }; 
这将被实例化如下:
 Foo<int, 1, 2> fi; 
要么
 Foo<char, 'a', 'b'> fc;