llvm-project/clang/test/SemaCXX/single-element-init-list.cpp
Mital Ashok a760df3165
[Clang] Implement CWG2137 (list-initialization from objects of the same type) (#94355)
[CWG2137](https://cplusplus.github.io/CWG/issues/2137.html)

This was previously implemented and then reverted in Clang 18 as #77768

This also implements a workaround for
[CWG2311](https://cplusplus.github.io/CWG/issues/2311.html), similarly
to the 2024-03-01 comment for
[CWG2742](https://cplusplus.github.io/CWG/issues/2742.html).

The exact wording this tries to implement, relative to the C++26 draft:

[over.match.list]p(1.2)

> Otherwise, or if no viable initializer-list constructor is found
<ins>and the initializer list does not consist of exactly a single
element with the same cv-unqualified class type as `T`</ins>, overload
resolution is performed again, [...]

[dcl.init.list]p(3.7)

> Otherwise, if `T` is a class type, constructors are considered. The
applicable constructors are enumerated and the best one is chosen
through overload resolution. <ins>If no constructor is found and the
initializer list consists of exactly a single element with the same
cv-unqualified class type as `T`, the object is initialized from that
element (by copy-initialization for copy-list-initialization, or by
direct-initialization for direct-list-initialization). Otherwise,</ins>
if a narrowing conversion (see below) is required [...]
2024-08-08 11:11:21 -04:00

83 lines
2.0 KiB
C++

// RUN: %clang_cc1 -std=c++17 -fsyntax-only -verify %s
// This is heavily affected by the speculative resolution applied to CWG2311
// So behaviour shown here is subject to change.
// expected-no-diagnostics
namespace std {
typedef decltype(sizeof(int)) size_t;
// libc++'s implementation
template <class _E>
class initializer_list
{
const _E* __begin_;
size_t __size_;
initializer_list(const _E* __b, size_t __s)
: __begin_(__b),
__size_(__s)
{}
public:
typedef _E value_type;
typedef const _E& reference;
typedef const _E& const_reference;
typedef size_t size_type;
typedef const _E* iterator;
typedef const _E* const_iterator;
constexpr initializer_list() : __begin_(nullptr), __size_(0) {}
constexpr size_t size() const {return __size_;}
const _E* begin() const {return __begin_;}
const _E* end() const {return __begin_ + __size_;}
};
template<typename T>
struct vector {
size_t sz;
constexpr vector() : sz(0) {}
constexpr vector(initializer_list<T> ilist) : sz(ilist.size()) {}
constexpr vector(const vector& other) : sz(other.sz) {}
constexpr std::size_t size() const { return sz; }
};
}
// https://github.com/llvm/llvm-project/pull/77768#issuecomment-1908062472
namespace Issue1 {
struct A {
constexpr A() {}
};
struct B {
int called_ctor;
constexpr explicit B(A) : called_ctor(0) {}
constexpr explicit B(std::vector<A>) : called_ctor(1) {}
};
struct C {
B b;
constexpr C() : b({A()}) {}
};
static_assert(C().b.called_ctor == 0);
}
// https://github.com/llvm/llvm-project/pull/77768#issuecomment-1957171805
namespace Issue2 {
struct A {
constexpr A(int x_) {}
constexpr A(const std::vector<A>& a) {}
};
void f() {
constexpr std::vector<A> a{1,2};
constexpr std::vector<A> b{a};
// -> constexpr std::vector<A> b(std::initializer_list<A>{ A(a) });
static_assert(b.size() == 1);
}
}