| 1 |
// RUN: %clang_cc1 -verify -std=c++1z %s |
| 2 |
|
| 3 |
namespace Explicit { |
| 4 |
// Each notional constructor is explicit if the function or function template |
| 5 |
// was generated from a constructor or deduction-guide that was declared explicit. |
| 6 |
template<typename T> struct A { |
| 7 |
A(T); |
| 8 |
A(T*); |
| 9 |
A(...); |
| 10 |
}; |
| 11 |
template<typename T> A(T) -> A<T>; |
| 12 |
template<typename T> explicit A(T*) -> A<T**>; // expected-note {{explicit}} |
| 13 |
|
| 14 |
int *p; |
| 15 |
A a(p); |
| 16 |
A b = p; |
| 17 |
A c{p}; |
| 18 |
A d = {p}; // expected-error {{selected an explicit deduction guide}} |
| 19 |
|
| 20 |
using X = A<int**>; |
| 21 |
using Y = A<int>; // uses the implicit guide, being more specialized than the eligible user-defined deduction guides. |
| 22 |
|
| 23 |
using X = decltype(a); |
| 24 |
using Y = decltype(b); |
| 25 |
using X = decltype(c); |
| 26 |
} |
| 27 |
|
| 28 |
|
| 29 |
namespace std { |
| 30 |
template<typename T> struct initializer_list { |
| 31 |
const T *ptr; |
| 32 |
__SIZE_TYPE__ size; |
| 33 |
initializer_list(); |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
namespace p0702r1 { |
| 38 |
template<typename T> struct X { // expected-note {{candidate}} expected-note {{implicit deduction guide}} |
| 39 |
X(std::initializer_list<T>); // expected-note {{candidate template ignored: could not match 'std::initializer_list<T>' against 'Z'}} \ |
| 40 |
// expected-note {{implicit deduction guide declared as 'template <typename T> X(std::initializer_list<T>) -> X<T>'}} |
| 41 |
}; |
| 42 |
|
| 43 |
X xi = {0}; |
| 44 |
X xxi = {xi}; |
| 45 |
extern X<int> xi; |
| 46 |
// Prior to P0702R1, this is X<X<int>>. |
| 47 |
extern X<int> xxi; |
| 48 |
|
| 49 |
struct Y : X<int> {}; |
| 50 |
Y y {{0}}; |
| 51 |
X xy {y}; |
| 52 |
extern X<int> xy; |
| 53 |
|
| 54 |
struct Z : X<int>, X<float> {}; |
| 55 |
Z z = {{0}, {0.0f}}; |
| 56 |
// This is not X<Z> even though that would work. Instead, it's ambiguous |
| 57 |
// between X<int> and X<float>. |
| 58 |
X xz = {z}; // expected-error {{no viable constructor or deduction guide}} |
| 59 |
} |
| 60 |
namespace pr34970 { |
| 61 |
//https://bugs.llvm.org/show_bug.cgi?id=34970 |
| 62 |
|
| 63 |
template <typename X, typename Y> struct IsSame { |
| 64 |
static constexpr bool value = false; |
| 65 |
}; |
| 66 |
|
| 67 |
template <typename Z> struct IsSame<Z, Z> { |
| 68 |
static constexpr bool value = true; |
| 69 |
}; |
| 70 |
|
| 71 |
template <typename T> struct Optional { |
| 72 |
template <typename U> Optional(U&&) { } |
| 73 |
}; |
| 74 |
|
| 75 |
template <typename A> Optional(A) -> Optional<A>; |
| 76 |
|
| 77 |
int main() { |
| 78 |
Optional opt(1729); |
| 79 |
Optional dupe(opt); |
| 80 |
|
| 81 |
static_assert(IsSame<decltype(opt), Optional<int>>::value); |
| 82 |
static_assert(IsSame<decltype(dupe), Optional<int>>::value); |
| 83 |
static_assert(!IsSame<decltype(dupe), Optional<Optional<int>>>::value); |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
namespace deduceTemplatedConstructor { |
| 91 |
template <typename X, typename Y> struct IsSame { |
| 92 |
static constexpr bool value = false; |
| 93 |
}; |
| 94 |
|
| 95 |
template <typename Z> struct IsSame<Z, Z> { |
| 96 |
static constexpr bool value = true; |
| 97 |
}; |
| 98 |
template <class T> struct A { |
| 99 |
using value_type = T; |
| 100 |
A(value_type); |
| 101 |
A(const A&); |
| 102 |
A(T, T, int); |
| 103 |
template<class U> |
| 104 |
A(int, T, U); |
| 105 |
}; |
| 106 |
|
| 107 |
A x(1, 2, 3); // no-error |
| 108 |
static_assert(IsSame<decltype(x),A<int>>::value); |
| 109 |
|
| 110 |
template <class T> |
| 111 |
A(T) -> A<T>; |
| 112 |
|
| 113 |
A a(42); |
| 114 |
static_assert(IsSame<decltype(a),A<int>>::value); |
| 115 |
A b = a; |
| 116 |
static_assert(IsSame<decltype(b),A<int>>::value); |
| 117 |
|
| 118 |
template <class T> |
| 119 |
A(A<T>) -> A<A<T>>; |
| 120 |
|
| 121 |
A b2 = a; |
| 122 |
static_assert(IsSame<decltype(b2),A<A<int>>>::value); |
| 123 |
} |