PageRenderTime 28ms CodeModel.GetById 5ms RepoModel.GetById 1ms app.codeStats 0ms

/test/CXX/temp/temp.decls/temp.variadic/p5.cpp

https://bitbucket.org/larsivi/amber-clang
C++ | 412 lines | 220 code | 94 blank | 98 comment | 7 complexity | 89597f4fa43b488fa8a2c24e6e49609e MD5 | raw file
  1. // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -std=c++11 -fblocks -fms-extensions -fsyntax-only -verify %s
  2. template<typename T, typename U> struct pair;
  3. template<typename ...> struct tuple;
  4. // A parameter pack whose name appears within the pattern of a pack
  5. // expansion is expanded by that pack expansion. An appearance of the
  6. // name of a parameter pack is only expanded by the innermost
  7. // enclosing pack expansion. The pattern of a pack expansion shall
  8. // name one or more parameter packs that are not expanded by a nested
  9. // pack expansion.
  10. template<typename... Types>
  11. struct Expansion {
  12. typedef pair<Types..., int> expand_with_pacs; // okay
  13. typedef pair<Types, int...> expand_no_packs; // expected-error{{pack expansion does not contain any unexpanded parameter packs}}
  14. typedef pair<pair<Types..., int>..., int> expand_with_expanded_nested; // expected-error{{pack expansion does not contain any unexpanded parameter packs}}
  15. };
  16. // All of the parameter packs expanded by a pack expansion shall have
  17. // the same number of arguments specified.
  18. template<typename ...Types>
  19. struct ExpansionLengthMismatch {
  20. template<typename ...OtherTypes>
  21. struct Inner {
  22. typedef tuple<pair<Types, OtherTypes>...> type; // expected-error{{pack expansion contains parameter packs 'Types' and 'OtherTypes' that have different lengths (3 vs. 2)}}
  23. };
  24. };
  25. ExpansionLengthMismatch<int, long>::Inner<unsigned int, unsigned long>::type
  26. *il_pairs;
  27. tuple<pair<int, unsigned int>, pair<long, unsigned long> >*il_pairs_2 = il_pairs;
  28. ExpansionLengthMismatch<short, int, long>::Inner<unsigned int, unsigned long>::type // expected-note{{in instantiation of template class 'ExpansionLengthMismatch<short, int, long>::Inner<unsigned int, unsigned long>' requested here}}
  29. *il_pairs_bad;
  30. // An appearance of a name of a parameter pack that is not expanded is
  31. // ill-formed.
  32. // Test for unexpanded parameter packs in each of the type nodes.
  33. template<typename T, int N, typename ... Types>
  34. struct TestPPName
  35. : public Types, public T // expected-error{{base type contains unexpanded parameter pack 'Types'}}
  36. {
  37. // BuiltinType is uninteresting
  38. // FIXME: ComplexType is uninteresting?
  39. // PointerType
  40. typedef Types *types_pointer; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  41. // BlockPointerType
  42. typedef Types (^block_pointer_1)(int); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  43. typedef int (^block_pointer_2)(Types); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  44. // LValueReferenceType
  45. typedef Types &lvalue_ref; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  46. // RValueReferenceType
  47. typedef Types &&rvalue_ref; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  48. // MemberPointerType
  49. typedef Types TestPPName::* member_pointer_1; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  50. typedef int Types::*member_pointer_2; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  51. // ConstantArrayType
  52. typedef Types constant_array[17]; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  53. // IncompleteArrayType
  54. typedef Types incomplete_array[]; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  55. // VariableArrayType
  56. void f(int i) {
  57. Types variable_array[i]; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  58. }
  59. // DependentSizedArrayType
  60. typedef Types dependent_sized_array[N]; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  61. // DependentSizedExtVectorType
  62. typedef Types dependent_sized_ext_vector __attribute__((ext_vector_type(N))); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  63. // VectorType is uninteresting
  64. // ExtVectorType
  65. typedef Types ext_vector __attribute__((ext_vector_type(4))); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  66. // FunctionProtoType
  67. typedef Types (function_type_1)(int); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  68. typedef int (function_type_2)(Types); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  69. // FunctionNoProtoType is uninteresting
  70. // UnresolvedUsingType is uninteresting
  71. // ParenType is uninteresting
  72. // TypedefType is uninteresting
  73. // TypeOfExprType
  74. typedef __typeof__((static_cast<Types>(0))) typeof_expr; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  75. // TypeOfType
  76. typedef __typeof__(Types) typeof_type; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  77. // DecltypeType
  78. typedef decltype((static_cast<Types>(0))) typeof_expr; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  79. // RecordType is uninteresting
  80. // EnumType is uninteresting
  81. // ElaboratedType is uninteresting
  82. // TemplateTypeParmType
  83. typedef Types template_type_parm; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  84. // SubstTemplateTypeParmType is uninteresting
  85. // TemplateSpecializationType
  86. typedef pair<Types, int> template_specialization; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  87. // InjectedClassName is uninteresting.
  88. // DependentNameType
  89. typedef typename Types::type dependent_name; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  90. // DependentTemplateSpecializationType
  91. typedef typename Types::template apply<int> dependent_name_1; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  92. typedef typename T::template apply<Types> dependent_name_2; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  93. // ObjCObjectType is uninteresting
  94. // ObjCInterfaceType is uninteresting
  95. // ObjCObjectPointerType is uninteresting
  96. };
  97. // FIXME: Test for unexpanded parameter packs in each of the expression nodes.
  98. template<int ...Values>
  99. void test_unexpanded_in_exprs() {
  100. // PredefinedExpr is uninteresting
  101. // DeclRefExpr
  102. Values; // expected-error{{expression contains unexpanded parameter pack 'Values'}}
  103. // IntegerLiteral is uninteresting
  104. // FloatingLiteral is uninteresting
  105. // ImaginaryLiteral is uninteresting
  106. // StringLiteral is uninteresting
  107. // CharacterLiteral is uninteresting
  108. (Values); // expected-error{{expression contains unexpanded parameter pack 'Values'}}
  109. // UnaryOperator
  110. -Values; // expected-error{{expression contains unexpanded parameter pack 'Values'}}
  111. // OffsetOfExpr
  112. struct OffsetMe {
  113. int array[17];
  114. };
  115. __builtin_offsetof(OffsetMe, array[Values]); // expected-error{{expression contains unexpanded parameter pack 'Values'}}
  116. // FIXME: continue this...
  117. }
  118. template<typename ... Types>
  119. void TestPPNameFunc(int i) {
  120. f(static_cast<Types>(i)); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  121. }
  122. template<typename T, template<class> class ...Meta>
  123. struct TestUnexpandedTTP {
  124. typedef tuple<typename Meta<T>::type> type; // expected-error{{declaration type contains unexpanded parameter pack 'Meta'}}
  125. };
  126. // Test for unexpanded parameter packs in declarations.
  127. template<typename T, typename... Types>
  128. // FIXME: this should test that the diagnostic reads "type contains..."
  129. struct alignas(Types) TestUnexpandedDecls : T{ // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  130. void member_function(Types); // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  131. void member_function () throw(Types); // expected-error{{exception type contains unexpanded parameter pack 'Types'}}
  132. operator Types() const; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  133. Types data_member; // expected-error{{data member type contains unexpanded parameter pack 'Types'}}
  134. static Types static_data_member; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  135. unsigned bit_field : static_cast<Types>(0); // expected-error{{bit-field size contains unexpanded parameter pack 'Types'}}
  136. static_assert(static_cast<Types>(0), "Boom"); // expected-error{{static assertion contains unexpanded parameter pack 'Types'}}
  137. enum E0 : Types { // expected-error{{fixed underlying type contains unexpanded parameter pack 'Types'}}
  138. EnumValue = static_cast<Types>(0) // expected-error{{enumerator value contains unexpanded parameter pack 'Types'}}
  139. };
  140. using typename Types::type; // expected-error{{using declaration contains unexpanded parameter pack 'Types'}}
  141. using Types::value; // expected-error{{using declaration contains unexpanded parameter pack 'Types'}}
  142. using T::operator Types; // expected-error{{using declaration contains unexpanded parameter pack 'Types'}}
  143. friend class Types::foo; // expected-error{{friend declaration contains unexpanded parameter pack 'Types'}}
  144. friend void friend_func(Types); // expected-error{{friend declaration contains unexpanded parameter pack 'Types'}}
  145. friend void Types::other_friend_func(int); // expected-error{{friend declaration contains unexpanded parameter pack 'Types'}}
  146. void test_initializers() {
  147. T copy_init = static_cast<Types>(0); // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
  148. T direct_init(0, static_cast<Types>(0)); // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
  149. T list_init = { static_cast<Types>(0) }; // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
  150. }
  151. T in_class_member_init = static_cast<Types>(0); // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
  152. TestUnexpandedDecls() :
  153. Types(static_cast<Types>(0)), // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
  154. Types(static_cast<Types>(0))...,
  155. in_class_member_init(static_cast<Types>(0)) {} // expected-error{{initializer contains unexpanded parameter pack 'Types'}}
  156. void default_function_args(T = static_cast<Types>(0)); // expected-error{{default argument contains unexpanded parameter pack 'Types'}}
  157. template<typename = Types*> // expected-error{{default argument contains unexpanded parameter pack 'Types'}}
  158. struct default_template_args_1;
  159. template<int = static_cast<Types>(0)> // expected-error{{default argument contains unexpanded parameter pack 'Types'}}
  160. struct default_template_args_2;
  161. template<template<typename> class = Types::template apply> // expected-error{{default argument contains unexpanded parameter pack 'Types'}}
  162. struct default_template_args_3;
  163. template<Types value> // expected-error{{non-type template parameter type contains unexpanded parameter pack 'Types'}}
  164. struct non_type_template_param_type;
  165. void decls_in_stmts() {
  166. Types t; // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  167. for (Types *t = 0; ; ) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  168. for (; Types *t = 0; ) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  169. T a[] = { T(), T(), T() };
  170. for (Types t : a) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  171. switch(Types *t = 0) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  172. while(Types *t = 0) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  173. if (Types *t = 0) { } // expected-error{{declaration type contains unexpanded parameter pack 'Types'}}
  174. try {
  175. } catch (Types*) { // expected-error{{exception type contains unexpanded parameter pack 'Types'}}
  176. }
  177. }
  178. };
  179. // FIXME: Test for unexpanded parameter packs in each of the statements.
  180. struct X {
  181. void f(int, int);
  182. template<typename ...Types>
  183. void f(Types...);
  184. };
  185. namespace std {
  186. class type_info;
  187. }
  188. typedef struct _GUID {
  189. unsigned long Data1;
  190. unsigned short Data2;
  191. unsigned short Data3;
  192. unsigned char Data4[ 8 ];
  193. } GUID;
  194. template<typename T, typename ...Types>
  195. void test_unexpanded_exprs(Types ...values) {
  196. // CXXOperatorCallExpr
  197. (void)(values + 0); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  198. (void)(0 + values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  199. // CXXMemberCallExpr
  200. values.f(); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  201. X x;
  202. x.f(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  203. x.Types::f(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  204. x.f<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  205. // CXXStaticCastExpr
  206. (void)static_cast<Types&>(values); // expected-error{{expression contains unexpanded parameter packs 'Types' and 'values'}}
  207. // CXXDynamicCastExpr
  208. (void)dynamic_cast<Types&>(values); // expected-error{{expression contains unexpanded parameter packs 'Types' and 'values'}}
  209. // CXXReinterpretCastExpr
  210. (void)reinterpret_cast<Types&>(values); // expected-error{{expression contains unexpanded parameter packs 'Types' and 'values'}}
  211. // CXXConstCastExpr
  212. (void)const_cast<Types&>(values); // expected-error{{expression contains unexpanded parameter packs 'Types' and 'values'}}
  213. // CXXTypeidExpr
  214. (void)typeid(Types); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  215. (void)typeid(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  216. // CXXUuidofExpr
  217. (void)__uuidof(Types); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  218. (void)__uuidof(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  219. // CXXThisExpr is uninteresting
  220. // CXXThrowExpr
  221. throw Types(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  222. throw values; // expected-error{{expression contains unexpanded parameter pack 'values'}}
  223. // CXXDefaultArgExpr is uninteresting
  224. // CXXBindTemporaryExpr is uninteresting
  225. // CXXConstructExpr is uninteresting
  226. // CXXFunctionalCastExpr
  227. (void)Types(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  228. // CXXTemporaryObjectExpr
  229. (void)X(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  230. // CXXScalarValueInitExpr is uninteresting
  231. // CXXNewExpr
  232. (void)new Types; // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  233. (void)new X(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  234. (void)new (values) X(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  235. (void)new X [values]; // expected-error{{expression contains unexpanded parameter pack 'values'}}
  236. // CXXDeleteExpr
  237. delete values; // expected-error{{expression contains unexpanded parameter pack 'values'}}
  238. delete [] values; // expected-error{{expression contains unexpanded parameter pack 'values'}}
  239. // CXXPseudoDestructorExpr
  240. T t;
  241. values.~T(); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  242. t.~Types(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  243. t.Types::~T(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  244. // UnaryTypeTraitExpr
  245. __is_pod(Types); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  246. // BinaryTypeTraitExpr
  247. __is_base_of(Types, T); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  248. __is_base_of(T, Types); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  249. // UnresolvedLookupExpr
  250. test_unexpanded_exprs(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  251. test_unexpanded_exprs<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  252. // DependentScopeDeclRefExpr
  253. Types::test_unexpanded_exprs(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  254. T::template test_unexpanded_exprs<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  255. // CXXUnresolvedConstructExpr
  256. Types(5); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  257. // CXXDependentScopeMemberExpr
  258. values.foo(); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  259. t.foo(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  260. // FIXME: There's an evil ambiguity here, because we don't know if
  261. // Types refers to the template type parameter pack in scope or a
  262. // non-pack member.
  263. // t.Types::foo();
  264. t.template foo<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  265. // UnresolvedMemberExpr
  266. x.f<Types>(); // expected-error{{expression contains unexpanded parameter pack 'Types'}}
  267. x.f(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  268. // CXXNoexceptExpr
  269. noexcept(values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  270. // PackExpansionExpr is uninteresting
  271. // SizeOfPackExpr is uninteresting
  272. // FIXME: Objective-C expressions will need to go elsewhere
  273. for (auto t : values) { } // expected-error{{expression contains unexpanded parameter pack 'values'}}
  274. switch (values) { } // expected-error{{expression contains unexpanded parameter pack 'values'}}
  275. do { } while (values); // expected-error{{expression contains unexpanded parameter pack 'values'}}
  276. test:
  277. goto *values; // expected-error{{expression contains unexpanded parameter pack 'values'}}
  278. void f(int arg = values); // expected-error{{default argument contains unexpanded parameter pack 'values'}}
  279. }
  280. // Test unexpanded parameter packs in partial specializations.
  281. template<typename ...Types>
  282. struct TestUnexpandedDecls<int, Types>; // expected-error{{partial specialization contains unexpanded parameter pack 'Types'}}
  283. // Test for diagnostics in the presence of multiple unexpanded
  284. // parameter packs.
  285. template<typename T, typename U> struct pair;
  286. template<typename ...OuterTypes>
  287. struct MemberTemplatePPNames {
  288. template<typename ...InnerTypes>
  289. struct Inner {
  290. typedef pair<OuterTypes, InnerTypes>* types; // expected-error{{declaration type contains unexpanded parameter packs 'OuterTypes' and 'InnerTypes'}}
  291. template<typename ...VeryInnerTypes>
  292. struct VeryInner {
  293. typedef pair<pair<VeryInnerTypes, OuterTypes>, pair<InnerTypes, OuterTypes> > types; // expected-error{{declaration type contains unexpanded parameter packs 'VeryInnerTypes', 'OuterTypes', ...}}
  294. };
  295. };
  296. };
  297. // Example from working paper
  298. namespace WorkingPaperExample {
  299. template<typename...> struct Tuple {};
  300. template<typename T1, typename T2> struct Pair {};
  301. template<class ... Args1> struct zip {
  302. template<class ... Args2> struct with {
  303. typedef Tuple<Pair<Args1, Args2> ... > type; // expected-error{{pack expansion contains parameter packs 'Args1' and 'Args2' that have different lengths (1 vs. 2)}}
  304. };
  305. };
  306. typedef zip<short, int>::with<unsigned short, unsigned>::type T1; // T1 is Tuple<Pair<short, unsigned short>, Pair<int, unsigned>>
  307. typedef Tuple<Pair<short, unsigned short>, Pair<int, unsigned>> T1;
  308. typedef zip<short>::with<unsigned short, unsigned>::type T2; // expected-note{{in instantiation of template class}}
  309. template<class ... Args> void f(Args...);
  310. template<class ... Args> void h(Args...);
  311. template<class ... Args>
  312. void g(Args ... args) {
  313. f(const_cast<const Args*>(&args)...); // OK: "Args" and "args" are expanded within f
  314. f(5 ...); // expected-error{{pack expansion does not contain any unexpanded parameter packs}}
  315. f(args); // expected-error{{expression contains unexpanded parameter pack 'args'}}
  316. f(h(args ...) + args ...);
  317. }
  318. }