|
From: | Juergen Sauermann |
Subject: | Re: [Bug-apl] cast from pointer to smaller type 'int' |
Date: | Thu, 21 Sep 2017 13:44:08 +0200 |
User-agent: | Mozilla/5.0 (X11; Linux i686; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 |
Hi, except that it did not compile on my machine:*Thread_context.cc:73:44: error: invalid cast from type ‘pthread_t {aka long unsigned int}’ to type ‘uint64_t {aka long long unsigned int}’**
** << reinterpret_cast<uint64_t>(thread)** ** ^** **make[1]: *** [apl-Thread_context.o] Error 1* This is primarily because *pthread_t *is not a pointer on my box. Best Regards, /// Jürgen On 09/20/2017 10:36 PM, Xiao-Yong Jin wrote:
reinterpret_cast works from smaller sizes to larger sizes. So simply reinterpret_cast<uint64_t *>(thread) should work for both of our machines (size_t is uint64_t for me). ... until some pthread implementation decides to hand you a larger sized struct for pthread_t.On Sep 20, 2017, at 3:26 PM, Juergen Sauermann <address@hidden> wrote: Hi Xiao-Yong, I can compile this: << reinterpret_cast<uint64_t>( reinterpret_cast<void *>(thread)) Please let me know if it compiles on you box as well. Best Regards, Jürgen On 09/20/2017 10:00 PM, Juergen Sauermann wrote:Hi Xiao-Yong, with reinterpret_cast<size_t> I am getting (gcc 4.8) this: Thread_context.cc: In member function ‘void Thread_context::print(std::ostream&) const’: Thread_context.cc:73:42: error: invalid cast from type ‘pthread_t {aka long unsigned int}’ to type ‘size_t {aka unsigned int}’ << reinterpret_cast<size_t>(thread) ^ make[1]: *** [apl-Thread_context.o] Error 1 It seems a little ridiculous to me that replacing a good old C-style cast that worked fine for the last 10 years cannot be replaced by one of the 3 members of the C++ zoo of casts in a portable way? Maybe some intermediate cast to const void * can be done on your machine? Best Regards, Jürgen On 09/19/2017 11:44 PM, Xiao-Yong Jin wrote:Should have got back to you sooner, but static_cast is not allowed between pointer types and non-pointer types. Thread_context.cc:73:11: error: static_cast from 'pthread_t' (aka '_opaque_pthread_t *') to 'int' is not allowed << static_cast<int>(thread) ^~~~~~~~~~~~~~~~~~~~~~~~ I need reinterpret_cast<size_t> here. I cannot reinterpret_cast<int> either, because of the difference in size.On Sep 11, 2017, at 3:01 PM, Juergen Sauermann <address@hidden> wrote: Hi Xiao-Yong, I see. In this particular case the pthread_t is only used to identify a thread and to distinguish it from other threads for debugging purposes. So as long as the compiler does not complain about the cast everything is fine. Cast to void * instead of int would also be an option. /// JürgenOn 09/11/2017 08:38 PM, Xiao-Yong Jin wrote:I don't think there is a portable way of printing a variable of type pthread_t. It could be a struct, depending on the implementation. static_cast<int> is alright, but may not be useful in the future.On Sep 11, 2017, at 1:08 PM, Juergen Sauermann <address@hidden> wrote: Hi Xiao-Yong, thanks, maybe fixed in SVN 1011. Problem with that error is that the casted type is not a pointer, at least on my machine. /// Jürgen On 09/11/2017 06:55 PM, Xiao-Yong Jin wrote:At revision 1010 Thread_context.cc:72:65: error: cast from pointer to smaller type 'int' loses information out << "thread #" << setw(2) << N << ":" << setw(16) << int(thread)
[Prev in Thread] | Current Thread | [Next in Thread] |