'if' statements on C++ template arguments <p>The following code gives me a warning when using the Intel compiler icpc13.</p> <pre><code>#include &lt;iostream&gt; template&lt;int N&gt; class base { public: double x[N]; }; template&lt;int N&gt; class derived : public base&lt;2*N&gt; { public: void print() { if (N==1) { std::cout &lt;&lt; this-&gt;x[1] &lt;&lt; std::endl; } else if (N==2) { std::cout &lt;&lt; this-&gt;x[3] &lt;&lt; std::endl; } } }; int main(int argc, char* argv[]) { derived&lt;1&gt; temp1; derived&lt;2&gt; temp2; temp1.print(); temp2.print(); } </code></pre> <blockquote> <p>Result: % icpc-13.1.163 main.cpp main.cpp(29): warning #175:</p> <p>subscript out of range std::cout&lt;x[3]&lt; <p>during instantiation of "void derived::print() [with N=1]" at line 41</p> </blockquote> <p>This is obviously not a danger since the if statement protects this line of code if the template argument is 1.</p> <p>I know that I "should" do template specialization for such things, but there is some shared code in the real functions that make the if statements on template arguments really handy.</p> <p>Question is…is this a "bad" thing to do, or is this incorrect compiler behavior? I don't get warnings with gcc, or xlc.</p> <p>I chose the pragma solution. This ends up looking like this:</p> <pre><code>void print() { #ifdef __INTEL_COMPILER #pragma warning push #pragma warning disable 175 #endif if (N==1) { std::cout&lt;&lt;this-&gt;x[1]&lt;&lt;std::endl; } else if (N==2) { std::cout &lt;&lt; this-&gt;x[3] &lt;&lt; std::endl; } #ifdef __INTEL_COMPILER #pragma warning pop #endif } }; </code></pre> <p>From what I can tell, the push saves the warning flags before the disable, and the pop restores them.</p>