| 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 |
// UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 |
|
| 11 |
// template<class T> |
| 12 |
// concept default_initializable = constructible_from<T> && |
| 13 |
// requires { T{}; } && |
| 14 |
// is-default-initializable<T>; |
| 15 |
|
| 16 |
#include <concepts> |
| 17 |
#include <cassert> |
| 18 |
|
| 19 |
#include "test_macros.h" |
| 20 |
|
| 21 |
template<class T> |
| 22 |
concept brace_initializable = requires { T{}; }; |
| 23 |
|
| 24 |
void test() { |
| 25 |
// LWG3149 |
| 26 |
// Changed the concept from constructible_from<T> |
| 27 |
// to constructible_from<T> && |
| 28 |
// requires { T{}; } && is-default-initializable <T> |
| 29 |
struct S0 { explicit S0() = default; }; |
| 30 |
S0 x0; |
| 31 |
S0 y0{}; |
| 32 |
static_assert(std::constructible_from<S0>); |
| 33 |
static_assert(brace_initializable<S0>); |
| 34 |
LIBCPP_STATIC_ASSERT(std::__default_initializable<S0>); |
| 35 |
static_assert(std::default_initializable<S0>); |
| 36 |
|
| 37 |
struct S1 { S0 x; }; // Note: aggregate |
| 38 |
S1 x1; |
| 39 |
S1 y1{}; // expected-error {{chosen constructor is explicit in copy-initialization}} |
| 40 |
static_assert(std::constructible_from<S1>); |
| 41 |
static_assert(!brace_initializable<S1>); |
| 42 |
LIBCPP_STATIC_ASSERT(std::__default_initializable<S1>); |
| 43 |
static_assert(!std::default_initializable<S1>); |
| 44 |
|
| 45 |
const int x2; // expected-error {{default initialization of an object of const type 'const int'}} |
| 46 |
const int y2{}; |
| 47 |
|
| 48 |
static_assert(std::constructible_from<const int>); |
| 49 |
static_assert(brace_initializable<const int>); |
| 50 |
LIBCPP_STATIC_ASSERT(!std::__default_initializable<const int>); |
| 51 |
static_assert(!std::default_initializable<const int>); |
| 52 |
|
| 53 |
const int x3[1]; // expected-error-re {{default initialization of an object of const type 'const int{{[ ]*}}[1]'}} |
| 54 |
const int y3[1]{}; |
| 55 |
static_assert(std::constructible_from<const int[1]>); |
| 56 |
static_assert(brace_initializable<const int[1]>); |
| 57 |
LIBCPP_STATIC_ASSERT(!std::__default_initializable<const int[1]>); |
| 58 |
static_assert(!std::default_initializable<const int[1]>); |
| 59 |
|
| 60 |
// Zero-length array extension |
| 61 |
const int x4[]; // expected-error {{definition of variable with array type needs an explicit size or an initializer}} |
| 62 |
const int y4[]{}; |
| 63 |
static_assert(!std::constructible_from<const int[]>); |
| 64 |
static_assert(brace_initializable<const int[]>); |
| 65 |
LIBCPP_STATIC_ASSERT(!std::__default_initializable<const int[]>); |
| 66 |
static_assert(!std::default_initializable<const int[]>); |
| 67 |
} |