Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7858153
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:17:24+00:00 2026-06-02T21:17:24+00:00

I came up with this after answering this question I had a simple function

  • 0

I came up with this after answering this question

I had a simple function template (C++11):

template<class elem_t, class list_t>
bool in_list(const elem_t& elem, const list_t& list) {
   for (const auto& i : list) {
      if (elem == i) {
         return true;
      }
   }
   return false;
}

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:

template<class elem_t>
bool in_list(const elem_t& elem, std::initializer_list<elem_t> list) {
   for (const auto& i : list) {
      if (elem == i) {
         return true;
      }
   }
   return false;
}

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?).

Is it standard behavior to accept this? And why?

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.

The warning message:

test.cpp: In function ‘int main()’:
test.cpp:33:43: warning: deducing ‘const list_t’ as ‘const std::initializer_list<int>’ [enabled by default]
test.cpp:6:6: warning:   in call to ‘bool in_list(const elem_t&, const list_t&) [with elem_t = int, list_t = std::initializer_list<int>]’ [enabled by default]
test.cpp:33:43: warning:   (you can disable this with -fno-deduce-init-list) [enabled by default]

When called by in_list(3, {1, 2, 3, 4, 5});

EDIT: Apparently deducing a template parameter as an initializer_list is an extension according to the working draft for my version of GCC (cite). 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!

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 how it was resolved.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:17:26+00:00Added an answer on June 2, 2026 at 9:17 pm

    Using what @Ben Voigt said in the comments on the other answer, I have gathered some relevant standard quotes:

    §14.5.6.2

    A function template can be overloaded with other function templates
    and with normal (non-template) functions. A normal function is not
    related to a function template (i.e., it is never considered to be a
    specialization
    ), even if it has the same name and type as a
    potentially generated function template specialization
    .

    So that rules out function template specialisation as what you’re doing, because even if two function template overloads could potentially generate the same function, it’s not specialisation. So it’s overloading.

    Such specializations are distinct functions and do not violate the one definition rule (3.2).

    So they’re distinct functions and that’s why it’s not erroring.

    §14.5.6.2.1

    If a function template is overloaded, the use of a function template
    specialization* might be ambiguous because template argument deduction
    (14.8.2) may associate the function template specialization with more
    than one function template declaration.

    This is priming is for what we both already saw, which is that in_list(a, b) where b is an initializer_list appears to match both function templates.

    (*Note that "function template specialization" here doesn’t mean specialising a function template, it means a function template that has been instantiated with a type. So with template<typename T> f();, f<int>() is a function template specialisation.)

    So we use what is called partial ordering of overloaded function templates to resolve this:

    Partial ordering of overloaded function template declarations is
    used in the following contexts to select the function template to which a function
    template specialization
    refers:

    — during overload resolution for a call to a function template specialization (13.3.3);

    — when the address of a function template specialization is taken;

    — when a placement operator delete that is a function template specialization is selected to match a placement operator new (3.7.4.2, 5.3.4);

    — when a friend function declaration (14.5.4), an explicit instantiation (14.7.2) or an explicit specialization (14.7.3) refers to a function template specialization.

    Ok, so that’s when partial ordering is for. This is what it does:

    Partial ordering selects which of two function templates is more
    specialized than the other by transforming each template in turn (see
    next paragraph) and performing template argument deduction using the
    function type. The deduction process determines whether one of the
    templates is more specialized than the other. If so, the more
    specialized template is the one chosen by the partial ordering
    process.

    And then you get into the long and laborious process of determining which template is more specialised, which you can read about if you want, but it’s really complicated and I probably don’t understand it all (and plus, I don’t have enough time to write about it :)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question came to mind after seeing this simple piece of code: if (!x%y)
I came across this question after searching for a ODBC or JDBC. To my
In trying to answer this question for myself I came across this nugget, after
After some research and about weeks of googgling I came to this question now:
Struggling still with this after hours or research.. I have a simple helper class
Well this question came after this one The reasons are pretty much the same,
I came across this after updating my SDK tools from revision 15 to 17,
After reading this: http://docs.djangoproject.com/en/dev/ref/contrib/csrf/#how-to-use-it I came to the conclusion that it is not valid
This came up recently in a class for which I am a teaching assistant.
I came across this question on an interview questions thread. Here is the question:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.