]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/pair/cons/explicit_construct.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / pair / cons / explicit_construct.cc
1 // { dg-do compile { target c++11 } }
2
3 // Copyright (C) 2015-2024 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 #include <utility>
21
22 struct Explicit
23 {
24 Explicit() = default;
25 explicit Explicit(int) {}
26 };
27
28 struct ExplicitDefault
29 {
30 explicit ExplicitDefault() {}
31 };
32
33 struct ExplicitDefaultDefault
34 {
35 explicit ExplicitDefaultDefault() = default;
36 };
37
38 std::pair<int, int> f1() {return {1,2};}
39
40 std::pair<Explicit, Explicit> f2() {return {1,2};} // { dg-error "explicit constructor" }
41
42 std::pair<long, long> f3() {return std::pair<int, int>{1,2};}
43
44 std::pair<Explicit, Explicit> f4()
45 {
46 return std::pair<int, int>{1,2}; // { dg-error "could not convert" }
47 }
48
49 std::pair<long, long> f5() {return {1,2};}
50
51 std::pair<int, int> v0{1,2};
52
53 std::pair<Explicit, Explicit> v1{1,2};
54
55 std::pair<Explicit, Explicit> v2 = {1,2}; // { dg-error "explicit constructor" }
56
57 std::pair<Explicit, Explicit> v3{std::pair<int,int>{1,2}};
58
59 std::pair<Explicit, Explicit> v4 =
60 std::pair<int,int>{1,2}; // { dg-error "conversion" }
61
62 std::pair<char *, char *> v5(0,0);
63
64 std::pair<long, long> v6{1,2};
65
66 std::pair<long, long> v7 = {1,2};
67
68 std::pair<long, long> v8{std::pair<int,int>{1,2}};
69
70 std::pair<long, long> v9 = std::pair<int,int>{1,2};
71
72 std::pair<Explicit, Explicit> v10{v0};
73
74 std::pair<Explicit, Explicit> v11 = v0; // { dg-error "conversion" }
75
76 std::pair<long, long> v12{v0};
77
78 std::pair<long, long> v13 = v0;
79
80 void f6(std::pair<Explicit, Explicit>) {}
81
82 void f7(std::pair<long, long>) {}
83
84 std::pair<ExplicitDefault, int> f8()
85 {
86 return {}; // { dg-error "convert" }
87 }
88
89 std::pair<ExplicitDefaultDefault, int> f9()
90 {
91 return {}; // { dg-error "convert" }
92 }
93
94 void f10(std::pair<ExplicitDefault, int>) {}
95
96 void f11(std::pair<ExplicitDefaultDefault, int>) {}
97
98 void test_arg_passing()
99 {
100 f6(v0); // { dg-error "could not convert" }
101 f6(v1);
102 f6({1,2}); // { dg-error "explicit constructor" }
103 f6(std::pair<Explicit, Explicit>{});
104 f6(std::pair<int, int>{}); // { dg-error "could not convert" }
105 f7(v0);
106 f7(v6);
107 f7({1,2});
108 f7(std::pair<int, int>{});
109 f7(std::pair<long, long>{});
110 f10({}); // { dg-error "convert" }
111 f11({}); // { dg-error "convert" }
112 f10(std::pair<ExplicitDefault, int>{});
113 f11(std::pair<ExplicitDefaultDefault, int>{});
114 }
115
116 struct MoveOnly
117 {
118 MoveOnly() = default;
119 MoveOnly(MoveOnly&&) {}
120 };
121
122 struct ExplicitMoveOnly
123 {
124 ExplicitMoveOnly() = default;
125 ExplicitMoveOnly(ExplicitMoveOnly&&) {}
126 explicit ExplicitMoveOnly(MoveOnly&&) {}
127 };
128
129 std::pair<int*, ExplicitMoveOnly> v14{nullptr, MoveOnly{}};
130 std::pair<ExplicitMoveOnly, int*> v15{MoveOnly{}, nullptr};
131
132 std::pair<int*, ExplicitMoveOnly> v16 =
133 {nullptr, MoveOnly{}}; // { dg-error "explicit constructor" }
134 std::pair<ExplicitMoveOnly, int*> v17 =
135 {MoveOnly{}, nullptr}; // { dg-error "explicit constructor" }