<p>How would you write (in C/C++) a macro which tests if an integer type (given as a parameter) is signed or unsigned?</p>

<pre>

      #define is_this_type_signed (my_type) ...

</pre>

[{'answer_id': 64908, 'author': 'ChrisN', 'author_id': 3853, 'author_profile': 'https://Stackoverflow.com/users/3853', 'pm_score': 5, 'selected': False, 'text': '<p>In C++, use <code>std::numeric_limits&lt;type&gt;::is_signed</code>.</p>\n<pre><code>#include &lt;limits&gt;\nstd::numeric_limits&lt;int&gt;::is_signed  - returns true\nstd::numeric_limits&lt;unsigned int&gt;::is_signed  - returns false\n</code></pre>\n<p>See <a href="https://en.cppreference.com/w/cpp/types/numeric_limits/is_signed" rel="nofollow noreferrer">https://en.cppreference.com/w/cpp/types/numeric_limits/is_signed</a>.</p>\n'}, {'answer_id': 64911, 'author': 'Fabio Ceconello', 'author_id': 8999, 'author_profile': 'https://Stackoverflow.com/users/8999', 'pm_score': 6, 'selected': True, 'text': '<p>If what you want is a simple macro, this should do the trick:</p>\n\n<pre><code>#define is_type_signed(my_type) (((my_type)-1) &lt; 0)\n</code></pre>\n'}, {'answer_id': 64915, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 0, 'selected': False, 'text': "<p>For c++, there is boost::is_unsigned&lt;T>. I'm curious why you need it though, there are few good reasons IMHO.</p>\n"}, {'answer_id': 64923, 'author': 'user7116', 'author_id': 7116, 'author_profile': 'https://Stackoverflow.com/users/7116', 'pm_score': 2, 'selected': False, 'text': "<p>Your requirement isn't exactly the best, but if you'd like to hack together a define, one option could be:</p>\n\n<pre><code>#define is_numeric_type_signed(typ) ( (((typ)0 - (typ)1)&lt;(typ)0) &amp;&amp; (((typ)0 - (typ)1) &lt; (typ)1) )\n</code></pre>\n\n<p>However, this isn't considered <em>nice</em> or portable by any means.</p>\n"}, {'answer_id': 64963, 'author': 'Frank Szczerba', 'author_id': 8964, 'author_profile': 'https://Stackoverflow.com/users/8964', 'pm_score': 1, 'selected': False, 'text': '<p>I was actually just wondering the same thing earlier today. The following seems to work:</p>\n\n<pre><code>#define is_signed(t)    ( ((t)-1) &lt; 0 )\n</code></pre>\n\n<p>I tested with:</p>\n\n<pre><code>#include &lt;stdio.h&gt;\n\n#define is_signed(t)    ( ((t)-1) &lt; 0 )\n#define psigned(t) printf( #t " is %s\\n", is_signed(t) ? "signed" : "unsigned" );\n\nint\nmain(void)\n{\n    psigned( int );\n    psigned( unsigned int );\n}\n</code></pre>\n\n<p>which prints:</p>\n\n<pre><code>int is signed\nunsigned int is unsigned\n</code></pre>\n'}, {'answer_id': 64964, 'author': 'Doug T.', 'author_id': 8123, 'author_profile': 'https://Stackoverflow.com/users/8123', 'pm_score': -1, 'selected': False, 'text': '<p>You could do this better with a template function, less macro nasty business.</p>\n\n<pre><code>    template &lt;typename T&gt;\n        bool IsSignedType()\n        {\n           // A lot of assumptions on T here\n           T instanceAsOne = 1;\n\n           if (-instanceAsOne &gt; 0)\n           {\n               return true;\n           }\n           else\n           {\n               return false;\n           }\n}\n</code></pre>\n\n<p>Forgive the formatting...</p>\n\n<p>I would try this out and see if it works...</p>\n'}, {'answer_id': 64985, 'author': 'Greg Rogers', 'author_id': 5963, 'author_profile': 'https://Stackoverflow.com/users/5963', 'pm_score': 1, 'selected': False, 'text': '<p>In C++ you can do:</p>\n\n<pre><code>\nbool is_signed = std::numeric_limits&lt;typeof(some_integer_variable)>::is_signed;\n</code></pre>\n\n<p>numeric_limits is defined in the &lt;limits> header.</p>\n'}, {'answer_id': 64988, 'author': 'Kevin', 'author_id': 6386, 'author_profile': 'https://Stackoverflow.com/users/6386', 'pm_score': -1, 'selected': False, 'text': "<p>In C, you can't write a macro that works on as-yet unknown typedef's of fundamental integer types.</p>\n\n<p>In C++, you can as long as your type is a fundamental integer type or a typedef of a fundamental integer type.  Here's what you'd do in C++:</p>\n\n<pre><code>template &lt;typename T&gt;\nstruct is_signed_integer\n{\n    static const bool value = false;\n};\n\ntemplate &lt;&gt;\nstruct is_signed_integer&lt;int&gt;\n{\n    static const bool value = true;\n};\n\ntemplate &lt;&gt;\nstruct is_signed_integer&lt;short&gt;\n{\n    static const bool value = true;\n};\n\ntemplate &lt;&gt;\nstruct is_signed_integer&lt;signed char&gt;\n{\n    static const bool value = true;\n};\n\ntemplate &lt;&gt;\nstruct is_signed_integer&lt;long&gt;\n{\n    static const bool value = true;\n};\n\n// assuming your C++ compiler supports 'long long'...\ntemplate &lt;&gt;\nstruct is_signed_integer&lt;long long&gt;\n{\n    static const bool value = true;\n};\n\n#define is_this_type_signed(my_type) is_signed_integer&lt;my_type&gt;::value\n</code></pre>\n"}, {'answer_id': 65240, 'author': 'Calliphony', 'author_id': 9050, 'author_profile': 'https://Stackoverflow.com/users/9050', 'pm_score': 2, 'selected': False, 'text': "<p>If you want a macro then this should do the trick:</p>\n\n<pre><code>#define IS_SIGNED( T ) (((T)-1)&lt;0)\n</code></pre>\n\n<p>Basically, cast -1 to your type and see if it's still -1. In C++ you don't need a macro. Just <code>#include &lt;limits&gt;</code> and:</p>\n\n<pre><code>bool my_type_is_signed = std::numeric_limits&lt;my_type&gt;::is_signed;\n</code></pre>\n"}, {'answer_id': 67109, 'author': 'Bronek', 'author_id': 10042, 'author_profile': 'https://Stackoverflow.com/users/10042', 'pm_score': 1, 'selected': False, 'text': '<p>Althout <code>typeof</code> is not legal C++ at the moment, you can use template deduction instead. See sample code below:</p>\n\n<pre><code>#include &lt;iostream&gt;\n#include &lt;limits&gt;\n\ntemplate &lt;typename T&gt;\nbool is_signed(const T&amp; t)\n{\n  return std::numeric_limits&lt;T&gt;::is_signed;\n}\n\nint main()\n{\n  std::cout &lt;&lt; \n    is_signed(1) &lt;&lt; " " &lt;&lt; \n    is_signed((unsigned char) 0) &lt;&lt; " " &lt;&lt; \n    is_signed((signed char) 0) &lt;&lt; std::endl;\n}\n</code></pre>\n\n<p>This code will print</p>\n\n<pre><code>  1 0 1\n</code></pre>\n'}, {'answer_id': 41602832, 'author': 'Mikhail', 'author_id': 314290, 'author_profile': 'https://Stackoverflow.com/users/314290', 'pm_score': 0, 'selected': False, 'text': '<p>A more "modern" approach is to use <code>type_traits</code>:</p>\n\n<pre><code>#include &lt;type_traits&gt;\n#include &lt;iostream&gt;\nint main()\n{\n    std::cout &lt;&lt; ( std::is_signed&lt;int&gt;::value ? "Signed" : "Unsigned") &lt;&lt;std::endl;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/64851', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4528/']