Clang Project

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