default-arguments-cxx0x.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s
  2. // RUN: %clang_cc1 -fsyntax-only -std=c++14 -verify %s
  3. // expected-no-diagnostics
  4. // Test default template arguments for function templates.
  5. template<typename T = int>
  6. void f0();
  7. template<typename T>
  8. void f0();
  9. void g0() {
  10. f0(); // okay!
  11. }
  12. template<typename T, int N = T::value>
  13. int &f1(T);
  14. float &f1(...);
  15. struct HasValue {
  16. static const int value = 17;
  17. };
  18. void g1() {
  19. float &fr = f1(15);
  20. int &ir = f1(HasValue());
  21. }
  22. namespace PR16689 {
  23. template <typename T1, typename T2> class tuple {
  24. public:
  25. template <typename = T2>
  26. constexpr tuple() {}
  27. };
  28. template <class X, class... Y> struct a : public X {
  29. using X::X;
  30. };
  31. auto x = a<tuple<int, int> >();
  32. }
  33. namespace PR16975 {
  34. template <typename...> struct is {
  35. constexpr operator bool() const { return false; }
  36. };
  37. template <typename... Types>
  38. struct bar {
  39. template <typename T,
  40. bool = is<Types...>()>
  41. bar(T);
  42. };
  43. bar<> foo{0};
  44. struct baz : public bar<> {
  45. using bar::bar;
  46. };
  47. baz data{0};
  48. }
  49. // rdar://23810407
  50. // An IRGen failure due to a symbol collision due to a default argument
  51. // being instantiated twice. Credit goes to Richard Smith for this
  52. // reduction to a -fsyntax-only failure.
  53. namespace rdar23810407 {
  54. // Instantiating the default argument multiple times will produce two
  55. // different lambda types and thus instantiate this function multiple
  56. // times, which will produce conflicting extern variable declarations.
  57. template<typename T> int f(T t) {
  58. extern T rdar23810407_variable;
  59. return 0;
  60. }
  61. template<typename T> int g(int a = f([] {}));
  62. void test() {
  63. g<int>();
  64. g<int>();
  65. }
  66. }
  67. // rdar://problem/24480205
  68. namespace PR13986 {
  69. constexpr unsigned Dynamic = 0;
  70. template <unsigned> class A { template <unsigned = Dynamic> void m_fn1(); };
  71. class Test {
  72. ~Test() {}
  73. A<1> m_target;
  74. };
  75. }
  76. // rdar://problem/34167492
  77. // Template B is instantiated during checking if defaulted A copy constructor
  78. // is constexpr. For this we check if S<int> copy constructor is constexpr. And
  79. // for this we check S constructor template with default argument that mentions
  80. // template B. In turn, template instantiation triggers checking defaulted
  81. // members exception spec. The problem is that it checks defaulted members not
  82. // for instantiated class only, but all defaulted members so far. In this case
  83. // we try to check exception spec for A default constructor which requires
  84. // initializer for the field _a. But initializers are added after constexpr
  85. // check so we reject the code because cannot find _a initializer.
  86. namespace rdar34167492 {
  87. template <typename T> struct B { using type = bool; };
  88. template <typename T> struct S {
  89. S() noexcept;
  90. template <typename U, typename B<U>::type = true>
  91. S(const S<U>&) noexcept;
  92. };
  93. class A {
  94. A() noexcept = default;
  95. A(const A&) noexcept = default;
  96. S<int> _a{};
  97. };
  98. }
  99. #if __cplusplus >= 201402L
  100. namespace lambda {
  101. // Verify that a default argument in a lambda can refer to the type of a
  102. // previous `auto` argument without crashing.
  103. template <class T>
  104. void bar() {
  105. (void) [](auto c, int x = sizeof(decltype(c))) {};
  106. }
  107. void foo() {
  108. bar<int>();
  109. }
  110. } // namespace lambda
  111. #endif