Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wctad-maybe-unsupported.C

raw
1 // Test -Wctad-maybe-unsupported.
2 // { dg-do compile { target c++17 } }
3 // { dg-options "-Wctad-maybe-unsupported" }
4
5 template <typename T> struct Empty { };
6
7 template <typename T>
8 struct A {
9 A(T); // generates 'template<class T> A(T)-> A<T>'
10 A(T, int); // generates 'template<class T> A(T, int)-> A<T>'
11 };
12
13 // These only succeed because of the implicit guide. That may be
14 // undesired.
15 A a1(42); // { dg-warning "may not intend to support class template argument deduction" }
16 A a2{42}; // { dg-warning "may not intend to support class template argument deduction" }
17 A a3 = {42}; // { dg-warning "may not intend to support class template argument deduction" }
18
19 template <typename T>
20 struct B {
21 B(T);
22 B(T, int);
23 };
24 template <typename T> B(T, int) -> B<Empty<T>>;
25
26 B b1(42);
27 B b2{42};
28 B b3 = {42};
29
30 // Motivating examples from Stephan Lavavej's 2018 CppCon talk.
31 template <class T, class U>
32 struct Pair {
33 T first;
34 U second;
35 explicit Pair(const T &t, const U &u) {}
36 };
37 // deduces to Pair<int, char[12]>
38 Pair p1(42, "hello world"); // { dg-warning "may not intend to support class template argument deduction" }
39 Pair p1b{42, "hello world"}; // { dg-warning "may not intend to support class template argument deduction" }
40
41 template <class T, class U>
42 struct Pair2 {
43 T first;
44 U second;
45 explicit Pair2(T t, U u) {}
46 };
47 // deduces to Pair2<int, const char*>
48 Pair2 p2(42, "hello world"); // { dg-warning "may not intend to support class template argument deduction" }
49 Pair2 p2b{42, "hello world"}; // { dg-warning "may not intend to support class template argument deduction" }
50
51 template <class T, class U>
52 struct Pair3 {
53 T first;
54 U second;
55 explicit Pair3(T const& t, U const& u) {}
56 };
57 template<class T1, class T2>
58 Pair3(T1, T2) -> Pair3<T1, T2>;
59 // deduces to Pair3<int, const char*>
60 Pair3 p3(42, "hello world");
61 static_assert(__is_same(decltype(p3), Pair3<int, const char*>));
62
63 // Test that explicit guides suppress the warning even if they
64 // aren't used as candidates.
65 template <typename T>
66 struct C {
67 C(T) { }
68 };
69 template <typename T>
70 explicit C(C<T> const&) -> C<void>;
71 C<int> c{42};
72 C c2 = c;
73 static_assert(__is_same(decltype(c2), C<int>));
74
75 // Clang's suppression test.
76 struct allow_ctad_t {
77 allow_ctad_t() = delete;
78 };
79
80 template <typename T>
81 struct S {
82 S(T) {}
83 };
84 S(allow_ctad_t) -> S<void>;
85 S s("abc");
86 S s2{"abc"};
87 static_assert(__is_same(decltype(s), S<const char *>));
88 static_assert(__is_same(decltype(s2), S<const char *>));