From possible implementation of std::addressof on https://en.cppreference.com/w/cpp/memory/addressof, it states that "correct implementation of std::addressof require compiler support". Why is it the case?
I tried out the implementation on https://godbolt.org/z/vnzbs9aTG and it worked as expected.
#include <iostream>
#include <string>
#include <type_traits>
template<class T>
typename std::enable_if<std::is_object<T>::value, T*>::type addressof_impl(T& arg) noexcept
{
return reinterpret_cast<T*>(
&const_cast<char&>(
reinterpret_cast<const volatile char&>(arg)));
}
template<class T>
typename std::enable_if<!std::is_object<T>::value, T*>::type addressof_impl(T& arg) noexcept
{
return &arg;
}
struct Student {
std::string name{};
int age{};
};
int main() {
Student s;
std::cout << addressof_impl(s);
return EXIT_SUCCESS;
}
std::addressof is supposed to be usable inside constant expressions. For example
constexpr int test() {
int x;
*std::addressof(x) = 0;
return x;
}
constexpr int j = test();
should be valid, but won't be with your implementation. Adding constexpr on your implementation won't help either, because evaluating reinterpret_cast always disqualifies an expression from being a constant expression.
Also, you are missing the deleted const rvalue overload to prevent taking the address of rvalues.
The above applies since C++17. If you are asking for C++11 as you tagged, then yes, that's a valid implementation for C++11. If you go through the standard library implementation links provided on the cppreference page, you'll also see that they used (minor variations of) the suggested implementation before C++17.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With