1762bb9d0ad20320b9f97a841dce57ba5e8e48b07Richard Smith// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
28e8fb3be5bd78f0564444eca02b404566a5f3b5dAndy Gibbs// expected-no-diagnostics
3a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
4a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// Metafunction to extract the Nth type from a set of types.
5a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<unsigned N, typename ...Types> struct get_nth_type;
6a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
7a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<unsigned N, typename Head, typename ...Tail>
8a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorstruct get_nth_type<N, Head, Tail...> : get_nth_type<N-1, Tail...> { };
9a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
10a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<typename Head, typename ...Tail>
11a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorstruct get_nth_type<0, Head, Tail...> {
12a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor  typedef Head type;
13a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor};
14a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
15a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// Placeholder type  when get_nth_type fails.
16a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorstruct no_type {};
17a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
18a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<unsigned N>
19a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorstruct get_nth_type<N> {
20a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor  typedef no_type type;
21a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor};
22a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
23a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<typename ...Args>
24a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortypename get_nth_type<0, Args...>::type first_arg(Args...);
25a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
26a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortemplate<typename ...Args>
27a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregortypename get_nth_type<1, Args...>::type second_arg(Args...);
28a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
29a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// Test explicit specification of function template arguments.
30a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregorvoid test_explicit_spec_simple() {
31a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor  int *ip1 = first_arg<int *>(0);
32a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor  int *ip2 = first_arg<int *, float*>(0, 0);
33a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor  float *fp1 = first_arg<float *, double*, int*>(0, 0, 0);
34a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor}
35a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
36a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// Template argument deduction can extend the sequence of template
37a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// arguments corresponding to a template parameter pack, even when the
38a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor// sequence contains explicitly specified template arguments.
39d3731198193eee92796ddeb493973b7a598b003eDouglas Gregorvoid test_explicit_spec_extension(double *dp) {
40a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor  int *ip1 = first_arg<int *>(0, 0);
41a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor  int *ip2 = first_arg<int *, float*>(0, 0, 0, 0);
42a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor  float *fp1 = first_arg<float *, double*, int*>(0, 0, 0);
43d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  int *i1 = second_arg<float *>(0, (int*)0, 0);
44d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  double *dp1 = first_arg<>(dp);
45a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor}
46a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor
473cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregortemplate<typename ...Types>
483cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorstruct tuple { };
493cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor
503cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregortemplate<typename ...Types>
513cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorvoid accept_tuple(tuple<Types...>);
523cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor
533cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorvoid test_explicit_spec_extension_targs(tuple<int, float, double> t3) {
543cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_tuple(t3);
553cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_tuple<int, float, double>(t3);
563cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_tuple<int>(t3);
573cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_tuple<int, float>(t3);
583cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor}
593cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor
603cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregortemplate<typename R, typename ...ParmTypes>
613cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorvoid accept_function_ptr(R(*)(ParmTypes...));
623cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor
633cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregorvoid test_explicit_spec_extension_funcparms(int (*f3)(int, float, double)) {
643cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr(f3);
653cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr<int>(f3);
663cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr<int, int>(f3);
673cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr<int, int, float>(f3);
683cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor  accept_function_ptr<int, int, float, double>(f3);
693cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor}
70