Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/addressof"

From cppreference.com
< cpp‎ | memory
(Notes: + ADL bomb)
 
(39 intermediate revisions by 9 users not shown)
Line 2: Line 2:
 
{{cpp/memory/navbar}}
 
{{cpp/memory/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | memory}}
+
{{dcl header|memory}}
{{dcl rev begin | num = 1}}
+
{{dcl|num=1|since=c++11|notes={{mark constexpr since c++17}}|
{{dcl | since=c++11|until=c++17 |1=
+
 
template< class T >
 
template< class T >
T* addressof(T& arg) noexcept;
+
T* addressof( T& arg ) noexcept;
 
}}
 
}}
{{dcl | since=c++17 |1=
+
{{dcl|since=c++11|num=2|1=
 
template< class T >
 
template< class T >
constexpr T* addressof(T& arg) noexcept;
+
const T* addressof( const T&& ) = delete;
const T* addressof(const T&&) = delete;
+
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ Obtains the actual address of the object or function {{tt|arg}}, even in presence of overloaded {{tt|operator&}}
+
@1@ Obtains the actual address of the object or function {{c|arg}}, even in presence of overloaded {{c|operator&}}.
 
@2@ Rvalue overload is deleted to prevent taking the address of {{c|const}} rvalues.
 
@2@ Rvalue overload is deleted to prevent taking the address of {{c|const}} rvalues.
  
{{rev begin}}
+
{{rrev|since=c++17|
{{rev|since=c++17|
+
The expression {{tt|std::addressof(e)}} is a [[cpp/language/constant_expression|constant subexpression]], if {{c|e}} is an lvalue constant subexpression.
The expression {{tt|std::addressof(E)}} is a [[cpp/language/constant_expression|constant subexpression]], if {{tt|E}} is an lvalue constant subexpression.
+
 
}}
 
}}
{{rev end}}
 
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | arg | lvalue object or function}}
+
{{par|arg|lvalue object or function}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
Pointer to {{tt|arg}}.
+
Pointer to {{c|arg}}.
 
+
===WARNINGS===
+
std::addressof is not available in freestanding environment. You cannot use std::move to write OS kernel or embedded system.
+
  
 
===Possible implementation===
 
===Possible implementation===
{{example
+
The implementation below is not {{c|constexpr}}, because {{c|reinterpret_cast}} is not usable in a constant expression. Compiler support is needed (see below).
| code=
+
{{eq fun
 +
|1=
 
template<class T>
 
template<class T>
constexpr auto addressof(T& arg) noexcept
+
typename std::enable_if<std::is_object<T>::value, T*>::type addressof(T& arg) noexcept
 
{
 
{
     return __builtin_addressof(arg);
+
     return reinterpret_cast<T*>(
 +
              &const_cast<char&>(
 +
                  reinterpret_cast<const volatile char&>(arg)));
 +
}
 +
 
 +
template<class T>
 +
typename std::enable_if<!std::is_object<T>::value, T*>::type addressof(T& arg) noexcept
 +
{
 +
    return &arg;
 
}
 
}
|
 
 
}}
 
}}
  
Note: the above implementation requires compiler support.
+
Correct implementation of this function requires compiler support: [https://github.com/gcc-mirror/gcc/blob/b8806796ec64585de39ca6ee3b7b30cc08f27d62/libstdc++-v3/include/bits/move.h#L47-L50 GNU libstdc++], [https://github.com/llvm/llvm-project/blob/5146b57b403b3a512dc64e766695b13803ef3b54/libcxx/include/__memory/addressof.h#L21-L28 LLVM libc++], [https://github.com/microsoft/STL/blob/1e312b38db8df1dfbea17adc344454feb8d00dd9/stl/inc/type_traits#L1548-L1551 Microsoft STL].
 +
 
 +
===Notes===
 +
{{feature test macro|__cpp_lib_addressof_constexpr|std=C++17|value=201603L|{{c|constexpr}} {{tt|std::addressof}}}}
 +
 
 +
{{c|constexpr}} for {{tt|addressof}} is added by {{wg21|LWG2296}}, and MSVC STL applies the change to C++14 mode as a defect report.
 +
 
 +
There are some weird cases where use of built-in {{c|operator&}} is ill-formed due to [[cpp/language/adl|argument-dependent lookup]] even if it is not overloaded, and {{tt|std::addressof}} can be used instead.
 +
 
 +
{{source|1=
 +
template<class T>
 +
struct holder { T t; };
 +
 
 +
struct incomp;
 +
 
 +
int main()
 +
{
 +
    holder<holder<incomp>*> x{};
 +
    // &x; // error: argument-dependent lookup attempts to instantiate holder<incomp>
 +
    std::addressof(x); // OK
 +
}
 +
}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| operator& may be overloaded for a pointer wrapper class to obtain a pointer to pointer:
+
|{{c|operator&}} may be overloaded for a pointer wrapper class to obtain a pointer to pointer:
| p=true
+
|p=true
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
 
#include <memory>
 
#include <memory>
  
 
template<class T>
 
template<class T>
struct Ptr {
+
struct Ptr
 +
{
 
     T* pad; // add pad to show difference between 'this' and 'data'
 
     T* pad; // add pad to show difference between 'this' and 'data'
 
     T* data;
 
     T* data;
     Ptr(T* arg) : pad(nullptr), data(arg)  
+
     Ptr(T* arg) : pad(nullptr), data(arg)
 
     {
 
     {
         std::cout << "Ctor this = " << this << std::endl;
+
         std::cout << "Ctor this = " << this << '\n';
 
     }
 
     }
  
Line 70: Line 93:
  
 
template<class T>
 
template<class T>
void f(Ptr<T>* p)  
+
void f(Ptr<T>* p)
 
{
 
{
 
     std::cout << "Ptr  overload called with p = " << p << '\n';
 
     std::cout << "Ptr  overload called with p = " << p << '\n';
 
}
 
}
  
void f(int** p)  
+
void f(int** p)
 
{
 
{
 
     std::cout << "int** overload called with p = " << p << '\n';
 
     std::cout << "int** overload called with p = " << p << '\n';
 
}
 
}
  
int main()  
+
int main()
 
{
 
{
 
     Ptr<int> p(new int(42));
 
     Ptr<int> p(new int(42));
     f(&p);                 // calls int** overload
+
     f(&p);               // calls int** overload
     f(std::addressof(p)); // calls Ptr<int>* overload, (= this)
+
     f(std::addressof(p)); // calls Ptr<int>* overload, (= this)
 
}
 
}
| output=
+
|output=
 
Ctor this = 0x7fff59ae6e88
 
Ctor this = 0x7fff59ae6e88
 
int** overload called with p = 0x7fff59ae6e90
 
int** overload called with p = 0x7fff59ae6e90
 
Ptr  overload called with p = 0x7fff59ae6e88
 
Ptr  overload called with p = 0x7fff59ae6e88
 
}}
 
}}
 +
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=2598|std=C++11|before={{c|std::addressof<const T>}} could take address of rvalues|after=disallowed by a deleted overload}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/memory/dsc allocator}}
+
{{dsc inc|cpp/memory/dsc allocator}}
{{dsc inc | cpp/memory/pointer_traits/dsc pointer_to}}
+
{{dsc inc|cpp/memory/pointer_traits/dsc pointer_to}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 01:46, 24 September 2024

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)



 
Defined in header <memory>
template< class T >
T* addressof( T& arg ) noexcept;
(1) (since C++11)
(constexpr since C++17)
template< class T >
const T* addressof( const T&& ) = delete;
(2) (since C++11)
1) Obtains the actual address of the object or function arg, even in presence of overloaded operator&.
2) Rvalue overload is deleted to prevent taking the address of const rvalues.

The expression std::addressof(e) is a constant subexpression, if e is an lvalue constant subexpression.

(since C++17)

Contents

[edit] Parameters

arg - lvalue object or function

[edit] Return value

Pointer to arg.

[edit] Possible implementation

The implementation below is not constexpr, because reinterpret_cast is not usable in a constant expression. Compiler support is needed (see below).

template<class T>
typename std::enable_if<std::is_object<T>::value, T*>::type addressof(T& arg) noexcept
{
    return reinterpret_cast<T*>(
               &const_cast<char&>(
                   reinterpret_cast<const volatile char&>(arg)));
}
 
template<class T>
typename std::enable_if<!std::is_object<T>::value, T*>::type addressof(T& arg) noexcept
{
    return &arg;
}

Correct implementation of this function requires compiler support: GNU libstdc++, LLVM libc++, Microsoft STL.

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_addressof_constexpr 201603L (C++17) constexpr std::addressof

constexpr for addressof is added by LWG2296, and MSVC STL applies the change to C++14 mode as a defect report.

There are some weird cases where use of built-in operator& is ill-formed due to argument-dependent lookup even if it is not overloaded, and std::addressof can be used instead.

template<class T>
struct holder { T t; };
 
struct incomp;
 
int main()
{
    holder<holder<incomp>*> x{};
    // &x; // error: argument-dependent lookup attempts to instantiate holder<incomp>
    std::addressof(x); // OK
}

[edit] Example

operator& may be overloaded for a pointer wrapper class to obtain a pointer to pointer:

#include <iostream>
#include <memory>
 
template<class T>
struct Ptr
{
    T* pad; // add pad to show difference between 'this' and 'data'
    T* data;
    Ptr(T* arg) : pad(nullptr), data(arg)
    {
        std::cout << "Ctor this = " << this << '\n';
    }
 
    ~Ptr() { delete data; }
    T** operator&() { return &data; }
};
 
template<class T>
void f(Ptr<T>* p)
{
    std::cout << "Ptr   overload called with p = " << p << '\n';
}
 
void f(int** p)
{
    std::cout << "int** overload called with p = " << p << '\n';
}
 
int main()
{
    Ptr<int> p(new int(42));
    f(&p);                // calls int** overload
    f(std::addressof(p)); // calls Ptr<int>* overload, (= this)
}

Possible output:

Ctor this = 0x7fff59ae6e88
int** overload called with p = 0x7fff59ae6e90
Ptr   overload called with p = 0x7fff59ae6e88

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2598 C++11 std::addressof<const T> could take address of rvalues disallowed by a deleted overload

[edit] See also

the default allocator
(class template) [edit]
[static]
obtains a dereferenceable pointer to its argument
(public static member function of std::pointer_traits<Ptr>) [edit]