Note that there are some explanatory texts on larger screens.

plurals
  1. POIs this partial function template specialization?
    text
    copied!<p>I came up with this after answering <a href="https://stackoverflow.com/questions/10369464/template-function-gives-no-matching-function-for-call-error">this question</a></p> <p>I had a simple function template (C++11):</p> <pre><code>template&lt;class elem_t, class list_t&gt; bool in_list(const elem_t&amp; elem, const list_t&amp; list) { for (const auto&amp; i : list) { if (elem == i) { return true; } } return false; } </code></pre> <p>But GCC emitted warnings because it doesn't seem to like deducing a template parameter as a std::initializer_list. So, without thinking, I made a specialization:</p> <pre><code>template&lt;class elem_t&gt; bool in_list(const elem_t&amp; elem, std::initializer_list&lt;elem_t&gt; list) { for (const auto&amp; i : list) { if (elem == i) { return true; } } return false; } </code></pre> <p>This worked. No more warnings. But when I looked again and thought about it, I remembered that C++ does not support partial template specialization on function templates. But that is what this appears to be. My only guess is that this is allowed because std::initializer_list is still dependent upon the template parameter, so it is, in essence, a different template. But I'm not sure if this is how it is supposed to be (isn't there a gotw about templates not overloading?).</p> <p>Is it standard behavior to accept this? And why?</p> <p>And as a bonus question, why does GCC not like deducing a template parameter as a std::initializer_list? It seems quite silly to expect me to copy and paste the code and just replace the parameter with a std::initializer_list.</p> <p>The warning message:</p> <pre><code>test.cpp: In function ‘int main()’: test.cpp:33:43: warning: deducing ‘const list_t’ as ‘const std::initializer_list&lt;int&gt;’ [enabled by default] test.cpp:6:6: warning: in call to ‘bool in_list(const elem_t&amp;, const list_t&amp;) [with elem_t = int, list_t = std::initializer_list&lt;int&gt;]’ [enabled by default] test.cpp:33:43: warning: (you can disable this with -fno-deduce-init-list) [enabled by default] </code></pre> <p>When called by <code>in_list(3, {1, 2, 3, 4, 5});</code></p> <p>EDIT: Apparently deducing a template parameter as an initializer_list is an extension according to the working draft for my version of GCC (<a href="http://gcc.gnu.org/onlinedocs/gcc-4.4.4/gcc/C_002b_002b-Dialect-Options.html" rel="nofollow noreferrer">cite</a>). So new question: Is this still an extension as of the final c++11 standard? If so, this would mean that it would be necessary for me to add the second function for standards-compliant code. Thanks for all your help! </p> <p>EDIT2: The compiler dialect flag appears to be removed for GCC 4.7, so it seems like the issue was resolved, but I don't know <em>how</em> it was resolved.</p>
 

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