What is the difference between std::addressof<>(var) and &var ?
int var = 42; std::cout << std::addressof(var); std::cout << '\n'; std::cout << &var; What is the difference between std::addressof<>(var) and &var ?
int var = 42; std::cout << std::addressof(var); std::cout << '\n'; std::cout << &var; The difference is that the unary operator & can be overloaded for class or enum types, do something foreign and return something that has nothing to do with the address of the object. And std::addressof always returns exactly the address of the object. For example, when writing a template code to get the address of an object of some generalized type T , you should use std::addressof , and not unary & , thus you protect your sample code from "surprises".
For int var there is no difference.
std::addressof cannot be used to get the address of a non-static method or a class field.
& ? - titanstruct foo{void bar1(void) {}}; auto p = ::std::addressof(foo::bar1); // ошибка struct foo{void bar1(void) {}}; auto p = ::std::addressof(foo::bar1); // ошибка struct foo{void bar1(void) {}}; auto p = ::std::addressof(foo::bar1); // ошибка - VTTstruct foo{void bar1(void) {}}; auto p = &foo::bar1; // ок struct foo{void bar1(void) {}}; auto p = &foo::bar1; // ок struct foo{void bar1(void) {}}; auto p = &foo::bar1; // ок - VTTstd::addressof(foo::bar1) has nothing to do with std::addressof . In C ++, foo::bar1 no longer a valid expression in itself, even at the syntax level. In an expression, the syntax foo::bar1 can be applied only after & or before () . - AnTstruct foo{static void bar1(void) {}}; auto p = ::std::addressof(foo::bar1); // OK struct foo{static void bar1(void) {}}; auto p = ::std::addressof(foo::bar1); // OK struct foo{static void bar1(void) {}}; auto p = ::std::addressof(foo::bar1); // OK - VTTSource: https://ru.stackoverflow.com/questions/904084/
All Articles