| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++20 } } |
| 19 | // { dg-require-thread-fence "" } |
| 20 | // { dg-add-options no_pch } |
| 21 | |
| 22 | #include <atomic> |
| 23 | |
| 24 | #ifndef __cpp_lib_atomic_value_initialization |
| 25 | # error "Feature test macro for atomic value-initialization is missing" |
| 26 | #elif __cpp_lib_atomic_value_initialization < 201911L |
| 27 | # error "Feature test macro for atomic value-initialization has wrong value" |
| 28 | #endif |
| 29 | |
| 30 | #include <testsuite_hooks.h> |
| 31 | |
| 32 | struct A |
| 33 | { |
| 34 | constexpr A() : val(42) { } |
| 35 | int val; |
| 36 | }; |
| 37 | |
| 38 | constexpr std::atomic<A> a; |
| 39 | |
| 40 | void |
| 41 | test01() |
| 42 | { |
| 43 | VERIFY(a.load().val == 42); |
| 44 | static_assert(!std::is_nothrow_default_constructible_v<std::atomic<A>>); |
| 45 | } |
| 46 | |
| 47 | struct B |
| 48 | { |
| 49 | constexpr B() noexcept : val(99) { } |
| 50 | int val; |
| 51 | }; |
| 52 | |
| 53 | constexpr std::atomic<B> b; |
| 54 | |
| 55 | void |
| 56 | test02() |
| 57 | { |
| 58 | VERIFY(b.load().val == 99); |
| 59 | static_assert(std::is_nothrow_default_constructible_v<std::atomic<B>>); |
| 60 | } |
| 61 | |
| 62 | constexpr std::atomic<int*> c; |
| 63 | |
| 64 | void |
| 65 | test03() |
| 66 | { |
| 67 | VERIFY(c.load() == nullptr); |
| 68 | static_assert(std::is_nothrow_default_constructible_v<std::atomic<int*>>); |
| 69 | } |
| 70 | |
| 71 | int |
| 72 | main() |
| 73 | { |
| 74 | test01(); |
| 75 | test02(); |
| 76 | test03(); |
| 77 | } |