Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the correct implementation of std::addressof require compiler support?

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;
}
like image 440
cpp Avatar asked Oct 24 '25 01:10

cpp


1 Answers

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.

like image 184
user17732522 Avatar answered Oct 26 '25 15:10

user17732522