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 851585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:27:08+00:00 2026-05-15T07:27:08+00:00

I think everyone has experience working with a code like the following: void fun(Type1&);

  • 0

I think everyone has experience working with a code like the following:

void fun(Type1&);
void fun(Type2&);
vector<Type1> vec;
for_each(vec.begin(), vec.end(), fun);

Of course that won’t compile, because it’s not clear which function to be passed. And what’s your commonly-used solution to the problem?

I know this will work:

for_each(vec.begin(), vec.end(), (void(*)(Type1&))fun);

But any better ideas?

  • 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-05-15T07:27:09+00:00Added an answer on May 15, 2026 at 7:27 am

    One solution is to use template function:

    template<typename T>
    void fun(T&);
    // specialize fun for Type1 and Type2
    ...
    for_each(vec.begin(), vec.end(), fun<Type1>);
    

    The better way is to use functor with template operator():

    struct fun
    {
      template<typename T>
      void operator()(T&) const;
    };
    ...
    for_each(vec.begin(), vec.end(), fun()); // T for operator() will be deduced automatically
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It has been asked when MeteorJS will have auth in place... I think everyone
Once again my silliness has struck. I would like to thank everyone who helped
I think everyone knows this site http://pinterest.com/ and I don't want to create site
Right first of I would like to thank everyone for helping me out on
Hey everyone, just a quick thing, I have the hex to integer working, but
http://min.us/muKpIB16z Is that zsh? Could somebody share the PS1 code to everyone :P? Thank
I guess everyone has already heard the news about some key developers leaving the
I've been trying to learn more about access modifiers in java, and everyone has
There are several applications that use this control. One I think most everyone knows
Disclaimer: Hello all Python masters and fans. I would like to thank everyone for

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.