template.carbon 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/primitives.carbon
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/function/template.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/function/template.carbon
  12. // --- deduced_or_explicit.carbon
  13. library "[[@TEST_NAME]]";
  14. import Cpp inline '''
  15. template<typename T> T min(T a, T b) { return a < b ? a : b; }
  16. ''';
  17. let a: i32 = 1;
  18. let b: i32 = 2;
  19. let min_implicit: i32 = Cpp.min(a, b);
  20. let min_explicit: i32 = Cpp.min(i32, a, b);
  21. let min_explicit_literal: i32 = Cpp.min(i32, 1, 2);
  22. // --- template_template_arg.carbon
  23. library "[[@TEST_NAME]]";
  24. import Cpp inline '''
  25. template<typename> struct X;
  26. template<typename> struct Y;
  27. template<template<typename> typename... TT, typename... T> void f(T ...a) {}
  28. ''';
  29. fn Call() {
  30. let a: i32 = 0;
  31. let b: i32 = 0;
  32. // `a` and `b` are not classified as template arguments, so are passed as the
  33. // function parameter pack `a...`.
  34. Cpp.f(a, b);
  35. // `Cpp.X` and `Cpp.Y` are classified as template template arguments, so they
  36. // are passed as the template parameter pack `TT...`.
  37. Cpp.f(Cpp.X, Cpp.Y);
  38. // Pass some template arguments followed by some function arguments.
  39. Cpp.f(Cpp.X, Cpp.Y, a, b);
  40. Cpp.f(Cpp.X, a, b, a);
  41. }
  42. // --- nontype_template_arg.carbon
  43. library "[[@TEST_NAME]]";
  44. import Cpp inline '''
  45. template<int... I, typename... T> void f(T ...a) {}
  46. template<int I = 0, int J = 0, typename = void, typename... T> void g(T ...a) {}
  47. ''';
  48. fn Call() {
  49. let a: i32 = 0;
  50. let b: i32 = 0;
  51. Cpp.f(a, b);
  52. // TODO: There should be a way to indicate that the arguments are template
  53. // arguments, not function arguments, when calling `f`.
  54. Cpp.f(1, 2);
  55. // Two function arguments.
  56. Cpp.g(1, 2);
  57. // Three template arguments.
  58. Cpp.g(1, 2, i32);
  59. // Three template arguments and two function arguments.
  60. Cpp.g(1, 2, i32, 3, 4);
  61. }
  62. // --- fail_function_arg_not_template_arg.carbon
  63. library "[[@TEST_NAME]]";
  64. import Cpp inline '''
  65. template<int N> void f();
  66. ''';
  67. fn Call() {
  68. // We treat this as a function argument, even though that makes the call fail.
  69. // CHECK:STDERR: fail_function_arg_not_template_arg.carbon:[[@LINE+7]]:10: error: no matching function for call to 'f' [CppInteropParseError]
  70. // CHECK:STDERR: 17 | Cpp.f(1);
  71. // CHECK:STDERR: | ^
  72. // CHECK:STDERR: fail_function_arg_not_template_arg.carbon:[[@LINE-8]]:22: note: candidate function template not viable: requires 0 arguments, but 1 was provided [CppInteropParseNote]
  73. // CHECK:STDERR: 5 | template<int N> void f();
  74. // CHECK:STDERR: | ^
  75. // CHECK:STDERR:
  76. Cpp.f(1);
  77. }
  78. // --- function_arg_not_template_arg_overloaded.carbon
  79. library "[[@TEST_NAME]]";
  80. import Cpp inline '''
  81. template<int N> void f();
  82. void f(int n);
  83. ''';
  84. fn Call() {
  85. Cpp.f(1);
  86. }
  87. // --- overloaded_with_different_template_params.carbon
  88. library "[[@TEST_NAME]]";
  89. import Cpp inline '''
  90. template<typename T> struct X;
  91. struct Y;
  92. template<int N, typename T> void f();
  93. template<typename T> void f();
  94. template<template<typename> typename T> void f();
  95. ''';
  96. fn Call() {
  97. Cpp.f(1, i32);
  98. Cpp.f(Cpp.X);
  99. Cpp.f(Cpp.Y);
  100. }
  101. // --- only_call_templates_when_given_template_args.carbon
  102. library "[[@TEST_NAME]]";
  103. import Cpp inline '''
  104. struct NonTemplate {};
  105. struct Template {};
  106. NonTemplate f(unsigned char, unsigned char);
  107. template<typename = void> Template f(unsigned char, int = 0);
  108. ''';
  109. // Prefers the non-template because it has a better match for the second parameter.
  110. let x: Cpp.NonTemplate = Cpp.f(1 as u8, 2 as u8);
  111. // Calls the template; the non-template is not considered due to the template
  112. // argument `i32`.
  113. let y: Cpp.Template = Cpp.f(i32, 1 as u8);