Note that there are some explanatory texts on larger screens.

plurals
  1. POMacro compiles with GCC but not with VS11
    primarykey
    data
    text
    <p>I have written the following code to assist restricting my template functions to certain types, with meaningful error message when other types were used. I had the idea from a different question in stackoverflow, on which I still can't comment because I am new here.</p> <p>The macro compiles flawlessly under linux GCC but not with Visual Studio 2012.</p> <pre><code>#include &lt;string&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;cassert&gt; #include &lt;type_traits&gt; #define ISALLOWED(DerivedT) (std::is_same&lt;T, DerivedT&gt;::value)||(std::is_base_of&lt;T,DerivedT&gt;::value) #define FE_1(WHAT, X) WHAT(X) #define FE_2(WHAT, X, ...) WHAT(X) || FE_1(WHAT, __VA_ARGS__) #define FE_3(WHAT, X, ...) WHAT(X) || FE_2(WHAT, __VA_ARGS__) #define FE_4(WHAT, X, ...) WHAT(X) || FE_3(WHAT, __VA_ARGS__) #define FE_5(WHAT, X, ...) WHAT(X) || FE_4(WHAT, __VA_ARGS__) #define FE_6(WHAT, X, ...) WHAT(X) || FE_5(WHAT, __VA_ARGS__) //... repeat as needed #define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME #define FOR_EACH(action,...) \ GET_MACRO(__VA_ARGS__,FE_6,FE_5,FE_4,FE_3,FE_2,FE_1)(action,__VA_ARGS__) // this is where you need to add types #define ASSERTIOTYPES \ static_assert(FOR_EACH(ISALLOWED,\ int,double,std::string\ ),"Type not defined for this template."); template&lt;class T&gt; std::ostream &amp; operator&lt;&lt;(std::ostream &amp;os, const std::vector&lt;T&gt; &amp; v) { ASSERTIOTYPES; os &lt;&lt; "["; for (size_t i = 0; i &lt; v.size(); i++) { os &lt;&lt; v[i]; if (i != v.size() - 1) os &lt;&lt; ", "; } os &lt;&lt; "]"; return os; } </code></pre> <p>The error message is: error C2977: 'std::is_same' : too many template arguments</p> <p>And it appears only when I define ASSERTIOTYPES with more than one type, but when it is defined with only one type, for example:</p> <pre><code>#define ASSERTIOTYPES \ static_assert(FOR_EACH(ISALLOWED,\ int\ ),"Type not defined for this template."); </code></pre> <p>... the code compiles normally.</p> <p>Any idea how to solve this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload