This shows you the differences between two versions of the page.
cast [2016/10/26 13:35] |
cast [2022/04/16 12:22] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ==== reinterpret_cast ==== | ||
+ | === What is the proper way to initialize a pointer member in C++ (11)? === | ||
+ | |||
+ | |||
+ | Any hint to do proper const static member pointer initialization using C++ 11 and later ? | ||
+ | |||
+ | Source: | ||
+ | |||
+ | struct App { | ||
+ | static const App* ok; | ||
+ | static constexpr App* ko = reinterpret_cast<App*>(0xBADC0DE); // <= FTBFS | ||
+ | }; | ||
+ | const App* App::ok = reinterpret_cast<App*>(0xBADC0DE); | ||
+ | |||
+ | Try it on latest gcc or clang it will fail : | ||
+ | |||
+ | http://melpon.org/wandbox/permlink/IlXTqNiVoJtnFDg0 | ||
+ | http://cpp.sh/96mau | ||
+ | http://coliru.stacked-crooked.com/a/e031835a72ed2172 | ||
+ | |||
+ | The reason of fail is clear : reinterpret_cast and constexpr are exclusives according to C++11 standard (but was accepted in some compiler). | ||
+ | |||
+ | http://eel.is/c++draft/expr.const#2 | ||
+ | http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3797.pdf (p125) | ||
+ | |||
+ | But older 2 steps method still works but not single one (on gcc-6.2.0 at least). | ||
+ | |||
+ | This code looks a bit odd, I agree on that but how to get it working or clang or latest g++. | ||
+ | |||
+ | Declaration and init in 2 places can be a workaround but it does not fully explain (init to nullptr is working of course). | ||
+ | |||
+ | Related errors are : | ||
+ | |||
+ | error: 'constexpr' needed for in-class initialization of static data member | ||
+ | error: reinterpret_cast from integer to pointer | ||
+ | |||
+ | More resources: | ||
+ | |||
+ | reinterpret_cast<> and portabilty | ||
+ | |||
+ | |||
+ | * http://stackoverflow.com/questions/39441338/what-is-the-proper-way-to-initialize-a-pointer-member-in-c-11 | ||
+ | * http://stackoverflow.com/questions/23401489/reinterpret-cast-and-portabilty# | ||
+ | |||
+ | </code> | ||
+ | ==== C cast ==== | ||
+ | |||
+ | /// @author: http://rzr.online.fr/contact.htm | ||
+ | #include <cstdlib> | ||
+ | // #define FIX // uncomment | ||
+ | /// show why anonymous types are evil in C++ | ||
+ | struct foo { | ||
+ | #ifndef FIX | ||
+ | struct {} *m; // anonymous type make cast impossible in C++ (ok on C) | ||
+ | #else | ||
+ | struct foo_m {} *m; // this way the type is named and can be used | ||
+ | #endif | ||
+ | }; | ||
+ | int main(int /* argc*/ , char* /* argv*/ []) | ||
+ | { | ||
+ | struct foo o; o.m = 0; | ||
+ | #ifndef FIX | ||
+ | o.m = malloc(sizeof( o.m )); //error: | ||
+ | // invalid conversion from 'void*' to 'foo::<anonymous struct>*' | ||
+ | #else | ||
+ | o.m = | ||
+ | (struct foo::foo_m*) //mandatory in c++ | ||
+ | malloc(sizeof( o.m )); | ||
+ | #endif | ||
+ | return 0; | ||
+ | } | ||
+ | //#eof "$Id: rzr -- -- $" | ||
+ | |||
+ | * http://bugs.debian.org/447196 | ||
+ | * http://sourceforge.net/tracker/index.php?func=detail&aid=1816107&group_id=12349&atid=312349 | ||
+ | |||
+ | |||
+ | ==== reinterpret_cast ==== | ||
+ | |||
+ | |||
+ | error: reinterpret_cast from integer to pointer | ||
+ | |||
+ | * http://stackoverflow.com/questions/39441338/what-is-the-proper-way-to-intialize-a-pointer-member-in-c-11 | ||
+ | * http://stackoverflow.com/questions/10369606/constexpr-pointer-value# ConstExpr | ||
+ | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63715# ConsT | ||
+ | * http://stackoverflow.com/questions/27946241/workaround-for-gcc-4-9-constexpr-bug | ||
+ | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63715# CasT | ||
+ | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70172# ExpressioN | ||
+ | * http://stackoverflow.com/questions/32790106/passing-reference-pointing-to-struct-as-template-parameter | ||
+ | * http://stackoverflow.com/questions/13499658/constexpr-initializing-with-pointers# StatiC | ||
+ | * http://stackoverflow.com/questions/23401489/reinterpret-cast-and-portabilty# ReinterpretCast | ||
+ | |||
+ | |||
+ | error: 'constexpr' needed for in-class initialization of static data member | ||
+ | |||
+ | |||
+ | @TaG: [[CPlusPlus]] [[gcc]] |