Beyond the C++ Standard Library: An Introduction to Boost [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Beyond the C++ Standard Library: An Introduction to Boost [Electronic resources] - نسخه متنی

Bjorn Karlsson

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید

addressof


Header: "boost/utility.hpp"


When taking the address of a variable, we typically depend on the returned value to be, well, the address of the variable. However, it's technically possible to overload operator&, which means that evildoers may be on a mission to wreak havoc on your address-dependent code. boost::addressof is provided to get the address anyway, regardless of potential uses and misuses of operator overloading. By using some clever internal machinery, the template function addressof ensures that it gets to the actual object and its address.

Usage


To always be sure to get the real address of an object, use boost::addressof. It is defined in "boost/utility.hpp". It is used where operator& would otherwise be used, and it accepts an argument that is a reference to the type whose address should be taken.


#include "boost/utility.hpp"
class some_class {};
int main() {
some_class s;
some_class* p=boost::addressof(s);
}

Before seeing more details on how to use addressof, it is helpful to understand why and how operator& may not actually return the address of an object.

Quick Lesson for Evildoers


If you really, really, really need to overload operator&, or just want to experiment with the potential uses of operator overloading, it's actually quite easy. When overloading operator&, the semantics are always different from what most users (and functions!) expect, so don't do it just to be cute; do it for a very good reason or not at all. That said, here's a code-breaker for you:


class codebreaker {
public:
int operator&() const {
return 13;
}
};

With this class, anyone who tries to take the address of an instance of codebreaker is handed the magical number 13.


template <typename T> void print_address(const T& t) {
std::cout << "Address: " << (&t) << '\n';
}
int main() {
codebreaker c;
print_address(c);
}

It's not hard to do this, but are there good arguments for ever doing it in real code? Probably not, because it cannot be made safe except when using local classes. The reason for this is that while it is legal to take the address of an incomplete type, it is undefined behavior to do so on an incomplete class with a user-defined operator&. Because we cannot guarantee that this won't happen, we're better off not overloading operator&.

Quick Remedy for Others


Even when operator& is supplied by the class, it is possible to get to the real address of instances of the class. addressof performs some clever work[6] behind the scenes to get to the bottom of the address issue, regardless of any operator& chicanery. If you adjust the function (print_address) to make use of addressof, you'll get what we came here for:

[6] Also known as an ingenious hack.


template <typename T> void print_address(const T& t) {
std::cout << "&t: " << (&t) << '\n';
std::cout << "addressof(t): " << boost::addressof(t) << '\n';
}

When invoked, the function gives this output (or similar, because the exact address differs depending upon your system).


&t: 13
addressof(t): 0012FECB13

That's more like it! If there are scenarios where you know, or suspect, that operator& is provided by a class but you need to be really sure that you get the actual address (which is unlikely for an overloaded operator& or why else would it be overloaded in the first place?), use addressof.

Summary


There are not many potent arguments for overloading operator&,[7] but because it is possible, some people do it anyway. When writing code that relies on retrieving the actual address of objects, addressof can help by ensuring that the real address is returned. When writing generic code, there is no way of telling which types will be operated upon, so if the address of parameterized types needs to be taken, use addressof.

[7] Custom hardware device drivers notwithstanding.

Use addressof when you must retrieve the actual address of an object, regardless of the semantics for operator&.

/ 124