首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::addressof

Defined in header <memory>

?

?

?

(1)

?

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

(since C++11) (until C++17)

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

(since C++17)

template <class T> const T* addressof(const T&&) = delete;

(2)

(since C++17)

1%29获取对象或函数的实际地址。arg,即使在超载的情况下operator&

2%29 rvalue重载被删除,以防止使用const罗兹。

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

(since C++17)

参数

arg

-

lvalue object or function

返回值

指向arg...

例外

1%29

noexcept规格:

noexcept

可能的实施

模板<类T>T%2A地址%28 t&Arg%29{返回重解释[医]铸造<T%2A>%28及[医]CAST<char&>%28重新解释[医]铸造<Const挥发性焦炭&>%28 arg%29%29%29;}

*。

注:上述实施过于简化而不是constexpr%28,需要编译器支持%29。

运算符&可以重载指针包装类以获得指向指针的指针:

二次

代码语言:javascript
复制
#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 << std::endl;
    }
 
    ~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)
}

二次

可能的产出:

二次

代码语言:javascript
复制
Ctor this = 0x7fff59ae6e88
int** overload called with p = 0x7fff59ae6e90
Ptr   overload called with p = 0x7fff59ae6e88

二次

另见

allocator

the default allocator (class template)

pointer_to static

obtains a dereferenceable pointer to its argument (public static member function of std::pointer_traits)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com