◐ Shell
clean mode source ↗

std::addressof - cppreference.com

Da cppreference.com.

Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.

La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui.

Click here for the English version of this page

<metanoindex/>

<tbody> </tbody>

Elemento definito nell'header

<memory>

template< class T > T* addressof(T& arg);

(dal C++11)

Obtains the actual address of the object or function arg, even in presence of overloaded operator&

Parametri

arg - lvalue object or function

Valore di ritorno

Pointer to arg.

Eccezioni

Possibile implementazione

template< class T >
T* addressof(T& arg) {
    return (T*)&(char&)arg;
}

Esempio

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* data;
   Ptr(T* arg) : data(arg) {}
   ~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
}

Output:

int** overload called with p = 0012FF64
Ptr overload called with p = 0012FF64

Vedi anche

the default allocator
(classe template) [modifica]

fornisce informazioni su come i tipi di puntatore

Original:

provides information about pointer-like types

The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.


(classe template) [modifica]