日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++11中std::addressof的使用

發布時間:2023/11/27 生活经验 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++11中std::addressof的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++11中的std::addressof獲得一個對象的實際地址,即使 operator& 操作符已被重載。它常用于原本要使用 operator& 的地方,它接受一個參數,該參數為要獲得地址的那個對象的引用。一般,若operator &()也被重載且不一致的話,那么用std::addressof代替它。

std::addressof: defined in header <memory>,? address of object or function, returns the address of the object or function referenced by ref, obtains the actual address of the object or function arg. this function returns the address of ref even in the presence of an overloaded reference operator (operator&).

You use std::addressof when you have to. Sadly, "when you have to" includes anytime you are working in template code and want to turn a variable of unknown type T or T& into an honest-to-God pointer to that variable's memory.

You should use std::addressof instead of & in any template implementation taking the address of a user-defined object. Doing so is not a perfect solution as it might? cause unexpected? behavior with types relying on an overloaded& operator (some argue it is an appropriate and just? punishment for developers dabbling in this dark art) .? However, it is possible to? detect the conflict of address reporting and? template expectations in debug builds? with assert(std::addressof(t) == &t).

下面是從其他文章中copy的測試代碼,詳細內容介紹可以參考對應的reference:

#include "addressof.hpp"
#include <iostream>
#include <memory> // std::addressof// reference: http://en.cppreference.com/w/cpp/memory/addressof
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 test_addressof_1()
{Ptr<int> p(new int(42));f(&p);                 // calls int** overloadf(std::addressof(p));  // calls Ptr<int>* overload, (= this)return 0;
}// reference: http://www.cplusplus.com/reference/memory/addressof/
struct unreferenceable {int x;unreferenceable* operator&() { return nullptr; }
};void print(unreferenceable* m) {if (m) std::cout << m->x << '\n';else std::cout << "[null pointer]\n";
}int test_addressof_2()
{void(*pfn)(unreferenceable*) = &print; // void(*pfn)(unreferenceable*); pfn = &print;unreferenceable val{ 10 };unreferenceable* foo = &val;unreferenceable* bar = std::addressof(val);(*pfn)(foo);   // prints [null pointer](*pfn)(bar);   // prints 10return 0;
}/
// reference: http://cppisland.com/?p=414
class Buffer
{
private:static const size_t buffer_size = 256;int bufferId;char buffer[buffer_size];public:Buffer(int bufferId_) : bufferId(bufferId_) {}Buffer* operator&() { return reinterpret_cast<Buffer*> (&buffer); }  //BAD practice, only for illustration!
};template<typename T>
void getAddress(T t)
{std::cout << "Address returned by & operator: " << std::ios::hex << &t << "\n";std::cout << "Address returned by addressof: " << std::ios::hex << std::addressof(t) << "\n";
}int test_addressof_3()
{int a = 3;fprintf(stderr, "a &: %p, address of: %p\n", &a, std::addressof(a));Buffer b(1);std::cout << "Getting the address of a Buffer type: \n";getAddress(b);return 0;
}/
// reference: https://wizardforcel.gitbooks.io/beyond-stl/content/38.html
class codebreaker {
public:int operator&() const {return 13;}
};int test_addressof_4()
{codebreaker c;std::cout << "&c: " << (&c) << '\n';std::cout << "addressof(t): " << std::addressof(c) << '\n';return 0;
}

GitHub: https://github.com/fengbingchun/Messy_Test

總結

以上是生活随笔為你收集整理的C++11中std::addressof的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。