p9-0x.cpp revision d3731198193eee92796ddeb493973b7a598b003e
1a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// RUN: %clang_cc1 -std=c++0x -fsyntax-only -verify %s 2a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 3a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// Metafunction to extract the Nth type from a set of types. 4a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<unsigned N, typename ...Types> struct get_nth_type; 5a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 6a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<unsigned N, typename Head, typename ...Tail> 7a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorstruct get_nth_type<N, Head, Tail...> : get_nth_type<N-1, Tail...> { }; 8a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 9a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<typename Head, typename ...Tail> 10a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorstruct get_nth_type<0, Head, Tail...> { 11a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor typedef Head type; 12a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor}; 13a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 14a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// Placeholder type when get_nth_type fails. 15a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorstruct no_type {}; 16a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 17a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<unsigned N> 18a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorstruct get_nth_type<N> { 19a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor typedef no_type type; 20a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor}; 21a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 22a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<typename ...Args> 23a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortypename get_nth_type<0, Args...>::type first_arg(Args...); 24a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 25a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<typename ...Args> 26a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortypename get_nth_type<1, Args...>::type second_arg(Args...); 27a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 28a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// Test explicit specification of function template arguments. 29a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorvoid test_explicit_spec_simple() { 30a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor int *ip1 = first_arg<int *>(0); 31a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor int *ip2 = first_arg<int *, float*>(0, 0); 32a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor float *fp1 = first_arg<float *, double*, int*>(0, 0, 0); 33a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor} 34a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 35a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// Template argument deduction can extend the sequence of template 36a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// arguments corresponding to a template parameter pack, even when the 37a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// sequence contains explicitly specified template arguments. 38d3731198193eee92796ddeb493973b7a598b003eDouglas Gregorvoid test_explicit_spec_extension(double *dp) { 39a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor int *ip1 = first_arg<int *>(0, 0); 40a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor int *ip2 = first_arg<int *, float*>(0, 0, 0, 0); 41a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor float *fp1 = first_arg<float *, double*, int*>(0, 0, 0); 42d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor int *i1 = second_arg<float *>(0, (int*)0, 0); 43d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor double *dp1 = first_arg<>(dp); 44a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor} 45a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor 46