Is this a correct use of the std::cout and std::cin basefields/conversions? Considering the code that you can find at the bottom of this post, it's safe to say that this is a good example of how I can use different representations in different basefields correctly in C++ ?

I'm referring to the use of:


std::hex in the std::istringstream case
the use of std::cout.setf() and std::cout.unsetf() in the "output section"


In short is this correct for both input and output ?

This could possibly lead to some bug of some kind on some platforms ?

I also have a doubt about the use of an unsigned int to input a char, the basefield always cares about that ? I mean if I input f I would like to store 15 in decimals and not 102 from the ascii table.
(because a char is often implemented as an unsigned int)

Thanks.

// trying to determine what is the header available for unsigned ints
#if __cplusplus &lt; 201103L
#include &lt;stdint.h&gt;
#else
#include &lt;cstdint&gt;
#endif

#include &lt;cstdlib&gt;
#include &lt;string&gt;
#include &lt;map&gt;
#include &lt;bitset&gt;
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;sstream&gt;

const static uint32_t fieldWidth = 2;

int main(int argc, char **argv) {

  std::map&lt;std::string, uint32_t&gt; bucket;
  bucket["EAX"] = 0x0; // initializing bucket with default values
  bucket["EBX"] = 0x0;
  bucket["ECX"] = 0x0;
  bucket["EDX"] = 0x0;

  if (argc &gt;= 2) {
    switch (argc) {
    case 2:
      std::istringstream(argv[1]) &gt;&gt; std::hex &gt;&gt; bucket["EAX"];
      break;
    case 3:
      std::istringstream(argv[1]) &gt;&gt; std::hex &gt;&gt; bucket["EAX"];
      std::istringstream(argv[2]) &gt;&gt; std::hex &gt;&gt; bucket["EBX"];
      break;
    case 4:
      std::istringstream(argv[1]) &gt;&gt; std::hex &gt;&gt; bucket["EAX"];
      std::istringstream(argv[2]) &gt;&gt; std::hex &gt;&gt; bucket["EBX"];
      std::istringstream(argv[3]) &gt;&gt; std::hex &gt;&gt; bucket["ECX"];
      break;
    case 5:
      std::istringstream(argv[1]) &gt;&gt; std::hex &gt;&gt; bucket["EAX"];
      std::istringstream(argv[2]) &gt;&gt; std::hex &gt;&gt; bucket["EBX"];
      std::istringstream(argv[3]) &gt;&gt; std::hex &gt;&gt; bucket["ECX"];
      std::istringstream(argv[4]) &gt;&gt; std::hex &gt;&gt; bucket["EDX"];
      break;
    default:
      std::cout &lt;&lt; "Too many arguments, exiting now\n";
      exit(EXIT_FAILURE);
    }

  } else {
    std::cout &lt;&lt; "Not enough arguments, exiting now\n";
    exit(EXIT_FAILURE);
  }

  std::cout.setf(std::ios::hex, std::ios::basefield);
  std::cout.setf(std::ios::showbase);

  std::cout &lt;&lt; "\nExecuting with this HEX values :\n\n";
  std::cout &lt;&lt; "EAX = " &lt;&lt; std::hex &lt;&lt; bucket["EAX"] &lt;&lt; "\n"
            &lt;&lt; "EBX = " &lt;&lt; std::hex &lt;&lt; bucket["EBX"] &lt;&lt; "\n"
            &lt;&lt; "ECX = " &lt;&lt; std::hex &lt;&lt; bucket["ECX"] &lt;&lt; "\n"
            &lt;&lt; "EDX = " &lt;&lt; std::hex &lt;&lt; bucket["EDX"] &lt;&lt; "\n";

  std::cout.unsetf(std::ios::showbase);
  std::cout.unsetf(std::ios::hex);

  asm volatile("cpuid"
               : "=a"(bucket["EAX"]), "=b"(bucket["EBX"]), "=c"(bucket["ECX"]),
                 "=d"(bucket["EDX"])
               : "a"(bucket["EAX"]), "b"(bucket["EBX"]), "c"(bucket["ECX"]),
                 "d"(bucket["EDX"]));

  std::cout &lt;&lt; "\nThe results:\n";
  for (std::map&lt;std::string, uint32_t&gt;::const_iterator k = bucket.begin();
       k != bucket.end(); ++k) {

    std::cout &lt;&lt; "\n";
    std::cout &lt;&lt; k-&gt;first &lt;&lt; "\n";

    for (int c = 31; c &gt;= 0; c--) {
      std::cout &lt;&lt; std::setfill(' ') &lt;&lt; std::setw(fieldWidth) &lt;&lt; c;
    }

    std::cout &lt;&lt; "\n";

    std::string s(std::bitset&lt;32&gt;(k-&gt;second).to_string());

    for (std::string::const_iterator j = s.begin(); j != s.end(); ++j) {
      std::cout &lt;&lt; std::setfill(' ') &lt;&lt; std::setw(fieldWidth) &lt;&lt; *j;
    }

    std::cout &lt;&lt; "\n";
  }
  std::cout &lt;&lt; "\n";
  return (0);
}


original repository
it's safe to say that this is a good example of how I can use different representations in different basefields correctly in C++ ?


I presume you stream manipulators.


  I'm referring to the use of:


std::hex in the std::istringstream case
the use of std::cout.setf() and std::cout.unsetf() in the "output section"


These are examples of stream manipulators:

I would not do this:

#if __cplusplus &lt; 201103L
#include &lt;stdint.h&gt;
#else
#include &lt;cstdint&gt;
#endif


In one version you are putting the functions in the global namespace (stdint.h) the other you are putting the functions in the std namespace. This will cause problems and may give you different results on different compilers.

This seems like you are doing it the hard way:

 if (argc &gt;= 2) {
    switch (argc) {
    case 2:
      std::istringstream(argv[1]) &gt;&gt; std::hex &gt;&gt; bucket["EAX"];
      break;
    case 3:
      std::istringstream(argv[1]) &gt;&gt; std::hex &gt;&gt; bucket["EAX"];
      std::istringstream(argv[2]) &gt;&gt; std::hex &gt;&gt; bucket["EBX"];
      break;
    case 4:
      std::istringstream(argv[1]) &gt;&gt; std::hex &gt;&gt; bucket["EAX"];
      std::istringstream(argv[2]) &gt;&gt; std::hex &gt;&gt; bucket["EBX"];
      std::istringstream(argv[3]) &gt;&gt; std::hex &gt;&gt; bucket["ECX"];
      break;
    case 5:
      std::istringstream(argv[1]) &gt;&gt; std::hex &gt;&gt; bucket["EAX"];
      std::istringstream(argv[2]) &gt;&gt; std::hex &gt;&gt; bucket["EBX"];
      std::istringstream(argv[3]) &gt;&gt; std::hex &gt;&gt; bucket["ECX"];
      std::istringstream(argv[4]) &gt;&gt; std::hex &gt;&gt; bucket["EDX"];
      break;
    default:
      std::cout &lt;&lt; "Too many arguments, exiting now\n";
      exit(EXIT_FAILURE);
    }


Why not a loop?

 if (argc &lt; 2 || argc &gt; 4) {/*ERROR MSG AND EXIT*/}

 std::vector&lt;std::string&gt;  mapIndex = { "EAX", "EBX", "ECX", "EDX" };
 for(int loop = 1; loop &lt; argc; ++loop)
 {
     // PS: just using mapIndex to keep it as close to your original
     //     code as possbile. If I had done this from scratch I would
     //     have just put the values into a four element array.

     std::istringstream(argv[loop]) &gt;&gt; std::hex &gt;&gt; bucket[mapIndex[loop-1]];
 }


The use of std::hex in this situation is correct.
It marks the input stream and the next value (if an hex integer) is correctly read.