1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -std=c++11 -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
463cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregortemplate<typename ...Types>
473cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorstruct tuple { };
483cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor
493cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregortemplate<typename ...Types>
503cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorvoid accept_tuple(tuple<Types...>);
513cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor
523cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorvoid test_explicit_spec_extension_targs(tuple<int, float, double> t3) {
533cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_tuple(t3);
543cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_tuple<int, float, double>(t3);
553cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_tuple<int>(t3);
563cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_tuple<int, float>(t3);
573cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor}
583cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor
593cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregortemplate<typename R, typename ...ParmTypes>
603cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorvoid accept_function_ptr(R(*)(ParmTypes...));
613cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor
623cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorvoid test_explicit_spec_extension_funcparms(int (*f3)(int, float, double)) {
633cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr(f3);
643cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr<int>(f3);
653cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr<int, int>(f3);
663cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr<int, int, float>(f3);
673cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr<int, int, float, double>(f3);
683cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor}
69