c++ complaining
In C++ you can use the address-of operator & to find the address of any lvalue†. However, due to operator overloading this is not necessarily true. So what do you if you overload &?
C++, as usual, has introduced a solution for the problem it created with the standard library function std::addressof. How is this implemented, you may ask? Well, as you have to do is write the following elegant, readable, and simple snippet:
template<typename _Tp>
inline _Tp*
__addressof(_Tp& __r) _GLIBCXX_NOEXCEPT
{
return reinterpret_cast<_Tp*>
(&const_cast<char&>(reinterpret_cast<const volatile char&>(__r)));
}
† this is actually a lie. function names are considered lvalues. However, if you overload a function, then the program fails to compile if you provide that name to std::addressof because C++ is a poorly designed language that makes no sense.
re: c++ complaining, wall of text
@wallhackio i think you have a serious conceptual error here. casting to char cannot pawsibly avoid alignment issues as a char is typically large enough to hold ASCII, and thus one byte. in C, it was (sort of) char * that can repurresent arbitrary data because that could repurresent an array of any arbitrary number of bytes. (nowadays you Should use void * fur this because the standard guarantees it actually does have the memory layout you would expect)
the function is not doing a bit-preserving cast to char; this is obviously impawsible. it is doing a bit-preserving cast to char&, which i assume works enough like a char pointer that it can give the programmer an arbitrary view of data of arbitrarily large size
re: c++ complaining, wall of text
@aescling A reference is treated as an alias to some other preexisting value. A char reference is not a pointer. It is an alias for some preexisting char.
re: c++ complaining, wall of text
@wallhackio if a char refurence is an alias to a preexisting char, then how can you cast data larger than one byte to a char refurence?
re: c++ complaining, wall of text
@wallhackio in your wall of text you described the function’a casting to char& as casting to char. but casting to char and casting to char& are not the same thing. i am stubbornly asking why casting to char is impawsible to draw your attention to this mistake
re: c++ complaining, wall of text
me: how does this work if you cast to char?
you: this is how casting to char& works
me: but you said the function is casting to char, which is not that
re: c++ complaining, wall of text
@aescling the image i drew is what would happen if you casted to char.
re: c++ complaining, wall of text
@aescling reinterpret_casting to char or to char& is the exact same thing except whether or not the result is treated as an lvalue or rvalue.
re: c++ complaining, wall of text
@wallhackio that is an extremely meaningful diffurence!!
re: c++ complaining, wall of text
@aescling you are making the rookie mistake of assuming that C++ makes sense
re: c++ complaining, wall of text
@wallhackio i am looking at the C++ Refrence page fur reinterpret_cast and it’s implying you can’t even cast to char: https://www.en.cppreference.com/w/cpp/language/reinterpret_cast.html
re: c++ complaining, wall of text
@wallhackio no
re: c++ complaining, wall of text
@aescling why not
re: c++ complaining, wall of text
@wallhackio that only makes sense if you are casting to a char * or char&. otherwise you will store a copy of that truncated data in a new location fur the new char
re: c++ complaining, wall of text
@aescling reinterpret_cast tries not to make a copy of anything. it is looking at the same region of memory but interpreting it differently. that's what it does, that's the whole point
re: c++ complaining, wall of text
@aescling according to this stackexchange answer https://stackoverflow.com/a/5924278/22334683
"reinterpret_cast<T&>(x) is the same thing as *reinterpret_cast<T*>(&x)"
re: c++ complaining, wall of text
@wallhackio that only makes sense if you try to cast arbitrary data to char&!! trying to cast to char makes no sense!!!!!!
re: c++ complaining, wall of text
@wallhackio so you can reinterpret_cast to char * or char&
doing that cast to char is not what you are describing
re: c++ complaining, wall of text
@wallhackio you are describing a char pointer and telling me it is a char
re: c++ complaining, wall of text
@aescling you are right, you can only reinterpret cast to a char pointer or reference. i was mistaken about the existence of reinterpret casts to char
I don't understand how the result is a char pointer. It is not. It is a char reference. References are aliases. They are like macros to the original thing. They aren't a number representing a location in memory. They are another name for the variable itself
re: c++ complaining, wall of text
@wallhackio it does not make sense fur it to act like anything else than a char pointer so idk why they use an explicit char refurence instead of explicit char pointer
re: c++ complaining, wall of text
@aescling again I return to the statement "reinterpret_cast<T&>(x) is the same thing as *reinterpret_cast<T*>(&x)"
A reinterpret cast to a reference type is equivalent to a dereferenced reinterpret cast of a pointer. It is very explicitly not a pointer.
re: c++ complaining, wall of text
@wallhackio jesus christ that just makes the function as defined utterly mystifying to me
re: c++ complaining, wall of text
@aescling again I return to the statement "reinterpret_cast<T&>(x) is the same thing as *reinterpret_cast<T*>(&x)"
A reinterpret cast to a reference type is equivalent to a deferenced reinterpret cast of a pointer. It is very explicitly not a pointer.
re: c++ complaining, wall of text
So, the point here is that we want to cast whatever data type is given to the function as a char. Why a char?
you are saying the function is casting arbitrary data to char. you cannot cast arbitrary data to char exactly because a char (typically) only holds one byte. so, the function cannot actually doing what you wrote in your description that it is doing
re: c++ complaining, wall of text
@aescling @wallhackio “you should use void*” ??????????????????
re: c++ complaining, wall of text
@aescling @wallhackio if you want an arbitrary array of bytes you Should use unsigned char[], which is the type which represents an arbitrary array of bytes????
re: c++ complaining, wall of text
@aescling Yes, the minimum size of of a
charis 1 byte. I am fully aware of that.There is no type in C++ that can be smaller than 1 byte. In fact, every type is some stored as a integral number of bytes. So any type can be reinterpreted as a char string. I don't see what the misconception is here.