5

Is it possible to cout thread::id in a decimal or octal format?

std::cout << std::showbase;
cout << dec(or oct) << boost::this_thread::get_id() 

I got always hex, for example 0xdf08.

Ivan Kush
  • 2,958
  • 2
  • 23
  • 39
  • 3
    I guess `get_id` returns a pointer to a thread-specific data structure. Pointers are not affected by `dec` etc. – celtschk Jun 17 '13 at 19:55

1 Answers1

7

You should be able to specify what output format you want by using standard I/O manipulators:

#include <iomanip>

// ...

std::cout << std::oct << boost::this_thread::get_id() << std::endl;
//           ^^^^^^^^
//           Octal

std::cout << std::dec << boost::this_thread::get_id() << std::endl;
//           ^^^^^^^^
//           Decimal

std::cout << std::hex << boost::this_thread::get_id() << std::endl;
//           ^^^^^^^^
//           Hexadecimal

However, notice that a thread::id does not need to be a number. Also, it may be a number but may be printed to the standard output in a different way than just inserting that number into std::cout.

The C++11 Standard specification the overload of operator << accepting an std::thread::id (which I assume to behave similarly to Boost's correspondent overload for boost::thread::it), says:

[...] Inserts an unspecified text representation of id into out.

This means the representation may not be a number at all, in which case formatting manipulators such as std::hex,std::dec, or std::oct may not have any influence on it.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 1
    45.3k rep and you say "you should be able to"; how about "you can"? +1 anyway. – Bathsheba Jun 17 '13 at 19:39
  • I'd guess there's a cast missing, because `get_id()` seems to return a pointer like type or a type that has it's own output operator overloaded. – πάντα ῥεῖ Jun 17 '13 at 19:41
  • @Bathsheba: Because the Standard specification for the behavior of `std::thread:id`, which I assume to be very similar to the behavior of `boost::thread:id`, says that `operator <<` for `thread::id` "*Inserts an unspecified text representation of id into out*". So in theory it may not be a number, which means that manipulators may not affect it – Andy Prowl Jun 17 '13 at 19:41
  • @Bathsheba What exactly is your problem with the wording? –  Jun 17 '13 at 19:42
  • 1
    I wouldn't go as far to say "a problem" as demonstrated by my upvoting the answer. However "you should be able to" is a weaker statement than "you can". But it seems that Andy Prowl has adequately qualified his statement with his comment, so read in full means the words he chose in his answer were chosen skilfully. Another +1 if I could. – Bathsheba Jun 17 '13 at 19:46
  • Did I miss something or is this *exactly* what the OP is already doing (as written in the question)? Therefore the *"should be able"* is indeed much more appropriate (even if it doesn't help the situation either). – Christian Rau Jun 18 '13 at 07:53
  • @ChristianRau: I assumed the user wanted to know what are the manipulators that allows you to specify a base representation. Anyway, I will clarify the answer and add what I wrote a few comments above. – Andy Prowl Jun 18 '13 at 07:58
  • 1
    @AndyProwl *"I assumed the user wanted to know what are the manipulators that allows you to specify a base representation."* - I see, it's just that his code example already uses these (though without qualification, but so is his `cout`). – Christian Rau Jun 18 '13 at 08:00
  • @ChristianRau: Right, it makes sense. Sometimes I rush into answering before thoroughly reading through the text. I edited the answer, thank you. – Andy Prowl Jun 18 '13 at 08:03
  • Thank you very much for your answer. So in Boost 1.53 x64 Win the output always give hexadecimal ID value independently of manipulators. In VS2010 in Threads window I have the same ID. PS: Yes, I knew about manipulators, I should have added in my post qualifications in cout and dec. I also rush sometimes -__-. – Ivan Kush Jun 19 '13 at 13:26