| 1 |
// Copyright (C) 2016-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 compile { target c++17 } } |
| 19 |
// { dg-add-options no_pch } |
| 20 |
|
| 21 |
#include <utility> |
| 22 |
|
| 23 |
// LWG 2296 std::addressof should be constexpr |
| 24 |
|
| 25 |
#ifndef __cpp_lib_addressof_constexpr |
| 26 |
# error "Feature-test macro for constexpr addressof missing" |
| 27 |
#elif __cpp_lib_addressof_constexpr != 201603 |
| 28 |
# error "Feature-test macro for constexpr addressof has wrong value" |
| 29 |
#endif |
| 30 |
|
| 31 |
int i; |
| 32 |
constexpr int* pi = std::addressof(i); |
| 33 |
static_assert( pi == &i, "&i" ); |
| 34 |
|
| 35 |
struct X { |
| 36 |
void operator&(); |
| 37 |
constexpr X* addr() { return this; } |
| 38 |
constexpr const X* addr() const { return this; } |
| 39 |
}; |
| 40 |
X x; |
| 41 |
constexpr X* px = std::addressof(x); |
| 42 |
static_assert( px == x.addr(), "x.addr()" ); |
| 43 |
const X cx; |
| 44 |
constexpr const X* pcx = std::addressof(cx); |
| 45 |
static_assert( pcx == cx.addr(), "cx.addr()" ); |
| 46 |
|
| 47 |
void |
| 48 |
test01() |
| 49 |
{ |
| 50 |
static int i2; |
| 51 |
static_assert( std::addressof(i2) == &i2, "&i2" ); |
| 52 |
|
| 53 |
static X x2; |
| 54 |
static_assert( std::addressof(x2) == x2.addr(), "x2.addr()" ); |
| 55 |
} |