reinterpret-cast

A “hack” to get float template parameter working compiles but segfaulted on both g++ and clang

心不动则不痛 提交于 2019-12-05 16:35:46
I know why I can't use float as template parameter and how to set a static const float member of template class thanks to a numerator/denominator couple. But I was trying another "hack" based on reinterpret_cast to "emule" float template parameters from its IEEE754 hexadecimal writing. Here is the small piece of code : #include <iostream> #include <cstdint> template <uint32_t T> struct MyStruct { static const float value; }; template <uint32_t T> const float MyStruct<T>::value = *reinterpret_cast<float*>(T); int main() { typedef MyStruct<0x40490fdb> Test; std::cout << Test::value << std::endl;

Need clarifications in C-style, reinterpret, and const casts

丶灬走出姿态 提交于 2019-12-05 08:47:08
Am I right in assuming that C-style casts (which are discouraged) are nothing but reinterpret_casts? Using the latter is visually striking and easy to search when looking for nasty casts, and hence it's recommended over C-style casts? If casting away const using const_cast and writing to a originally const object is undefined, what is the purpose of const_cast? Note: I know that Bjarne rightly condemns casting operations that they are unsafe and even goes to the extent of stating "An ugly operation should have an ugly syntactic form." and hence the verbosity of casting operators in C++. So I

how convert std::array<char, N> to char (&dest)[N]?

北城余情 提交于 2019-12-05 08:38:54
What is the way to pass std::array<char, N> to such function: template<size_t N> void safe_func(char (&dest)[N]); ? I try this one: #include <array> template <size_t N> using SafeArray = char[N]; template <size_t N> void safe_func(char (&dest)[N]) {} int main() { SafeArray<10> a1; safe_func(a1); std::array<char, 10> a2; safe_func(*static_cast<SafeArray<10> *>(static_cast<void *>(a2.data()))); } It works, but I doubt, may be something wrong with my cast, and on other compiler or platform (I used gcc/linux/amd64), I faced with wrong reference? One way: template<class T, size_t N> using c_array =

Why is it allowed to reinterpret_cast integral, enumeration and pointer-to-member types to themselves?

风流意气都作罢 提交于 2019-12-05 08:06:59
In this recent question , we saw that it's not allowed to reinterpret_cast some custom class type instance to itself; struct A{}; reinterpret_cast<A>(A{}); is invalid (it works only through references or pointers). Which seems to make sense, because of the lack of real-world scenarios where such identity conversion is necessary. Checking the corresponding standard clause, we have in [expr.reinterpret.cast] (emphasis mine): 1 [...] Conversions that can be performed explicitly using reinterpret_­cast are listed below. No other conversion can be performed explicitly using reinterpret_­cast . 2 [.

Why do I need a reinterpret_cast to convert Fred ** const to void ** const?

点点圈 提交于 2019-12-05 00:34:49
I have a const pointer to a pointer to a Fred and I don't understand why a static_cast isn't sufficient. typedef struct { int n; } Fred; Fred *pFred; Fred **const ppFred = &pFred; void **const ppVoid = static_cast<void ** const>(ppFred); Please could someone explain why a reinterpret_cast is needed to convert a pointer to Fred* to a pointer to void* but static_cast is fine to convert pointer to Fred to a pointer to void . There's no requirement that a Fred* and a void* have the same size and representation. (I've worked on machines where they didn't, although that was before my C++ days.) When

How does an unspecified pointer conversion behave in C++14?

故事扮演 提交于 2019-12-04 05:49:38
The result of some pointer casts are described as unspecified. For example, [expr.static.cast]/13: A prvalue of type “pointer to cv1 void” can be converted to a prvalue of type “pointer to cv2 T,” [...] If the original pointer value represents the address A of a byte in memory and A satisfies the alignment requirement of T, then the resulting pointer value represents the same address as the original pointer value, that is, A. The result of any other such pointer conversion is unspecified . My question is: in the case where alignment is not satisfied, what are the possible results? For example,

reinterpret_cast vs strict aliasing

杀马特。学长 韩版系。学妹 提交于 2019-12-04 04:18:11
I was reading about strict aliasing, but its still kinda foggy and I am never sure where is the line of defined / undefined behaviour. The most detailed post i found concentrates on C. So it would be nice if you could tell me if this is allowed and what has changed since C++98/11/... #include <iostream> #include <cstring> template <typename T> T transform(T t); struct my_buffer { char data[128]; unsigned pos; my_buffer() : pos(0) {} void rewind() { pos = 0; } template <typename T> void push_via_pointer_cast(const T& t) { *reinterpret_cast<T*>(&data[pos]) = transform(t); pos += sizeof(T); }

Is it safe to reinterpret_cast an enum class variable to a reference of the underlying type?

人盡茶涼 提交于 2019-12-03 19:26:58
问题 I've seen reinterpret_cast used to apply incrementation to enum classes, and I'd like to know if this usage is acceptable in standard C++. enum class Foo : int8_t { Bar1, Bar2, Bar3, Bar4, First = Bar1, Last = Bar4 }; for (Foo foo = Foo::First; foo <= Foo::Last; ++reinterpret_cast<int8_t &>(foo)) { ... } I know casting to a reference of a base class is safe in case of trivial classes. But since enum classes are not event implicitly converted to their underlying types, I'm not sure if and how

Is %p specifier only for valid pointers?

余生颓废 提交于 2019-12-03 09:52:38
Suppose on my platform sizeof(int)==sizeof(void*) and I have this code: printf( "%p", rand() ); Will this be undefined behavior because of passing a value that is not a valid pointer in place of %p ? To expand upon @larsman's answer (which says that since you violated a constraint, the behavior is undefined), here's an actual C implementation where sizeof(int) == sizeof(void*) , yet the code is not equivalent to printf( "%p", (void*)rand() ); The Motorola 68000 processor has 16 registers which are used for general computation, but they are not equivalent. Eight of them (named a0 through a7 )

Is reinterpret_cast mostly useless?

佐手、 提交于 2019-12-03 05:15:11
I've read various previous questions about the use of reinterpret_cast , and I've also read the relevant wording in the C++ standard. Essentially, what it comes down to is that the result of a pointer-to-pointer reinterpret_cast operation can't safely be used for anything other than being cast back to the original pointer type. In practice, however, most real-world uses of reinterpret_cast seem to be based on the (wrong) assumption that a reinterpret_cast is the same as a C-style cast. For example, I've seen lots of code which uses reinterpret_cast to cast from char* to unsigned char* for the