1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // REQUIRES: c++11
10
11 // <utility>
12
13 // Test that only the default constructor is constexpr in C++11
14
15 #include <utility>
16 #include <cassert>
17
18 struct ExplicitT {
ExplicitTExplicitT19 constexpr explicit ExplicitT(int x) : value(x) {}
ExplicitTExplicitT20 constexpr explicit ExplicitT(ExplicitT const& o) : value(o.value) {}
21 int value;
22 };
23
24 struct ImplicitT {
ImplicitTImplicitT25 constexpr ImplicitT(int x) : value(x) {}
ImplicitTImplicitT26 constexpr ImplicitT(ImplicitT const& o) : value(o.value) {}
27 int value;
28 };
29
main(int,char **)30 int main(int, char**)
31 {
32 {
33 using P = std::pair<int, int>;
34 constexpr int x = 42;
35 constexpr P default_p{};
36 constexpr P copy_p(default_p);
37 constexpr P const_U_V(x, x); // expected-error {{must be initialized by a constant expression}}
38 constexpr P U_V(42, 101); // expected-error {{must be initialized by a constant expression}}
39 }
40 {
41 using P = std::pair<ExplicitT, ExplicitT>;
42 constexpr std::pair<int, int> other;
43 constexpr ExplicitT e(99);
44 constexpr P const_U_V(e, e); // expected-error {{must be initialized by a constant expression}}
45 constexpr P U_V(42, 101); // expected-error {{must be initialized by a constant expression}}
46 constexpr P pair_U_V(other); // expected-error {{must be initialized by a constant expression}}
47 }
48 {
49 using P = std::pair<ImplicitT, ImplicitT>;
50 constexpr std::pair<int, int> other;
51 constexpr ImplicitT i = 99;
52 constexpr P const_U_V = {i, i}; // expected-error {{must be initialized by a constant expression}}
53 constexpr P U_V = {42, 101}; // expected-error {{must be initialized by a constant expression}}
54 constexpr P pair_U_V = other; // expected-error {{must be initialized by a constant expression}}
55 }
56
57 return 0;
58 }
59