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

The Archive Base Latest Questions

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

I have been playing around with variadic templates in the new c++ standard and

  • 0

I have been playing around with variadic templates in the new c++ standard and came up with a map function (headers + using decs excluded):

template<typename T>
T square(T i)
{
        return i * i;
}

template <typename T, typename... Ts>
const tuple<Ts...> map(const T f, const Ts...args)
{
        return make_tuple(f(args)...);
}

int main(int c, char *argv[])
{
        tuple<int, int> t;
        int (*fp) (int) = square;

        t = map(fp, 6, 8);

        cout <<get<0>(t) <<endl;
        cout <<get<1>(t) <<endl;

        return 0;
}

Which works. As long as all the arguments are the same type for map. If I change the main to use a slightly more general form:

 tuple<int, float> t;

 t = map(square, 6, 8.0f);

gcc 4.4 reports:

In function ‘int main(int, char**)’:
error: no matching function for call to ‘map(<unresolved overloaded function type>, int, float)’

Any ideas how to make this work?

  • 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-27T03:44:15+00:00Added an answer on May 27, 2026 at 3:44 am

    First, you can’t pass around an unresolved function template as a pointer (or template parameter), you can only pass around instances of it. What that means is that your template’s first argument is being passed as an int (*)(int) in this example, and it cannot call the float (*)(float) instantiation. I’m not sure of the best way to fix that, but anyway it’s not technically what you asked about.

    I don’t have a compiler to test this on, but I think if you use std::function to infer the types that the function you are passing in wants, you might be able to cast the parameters to the function. Like this:

    template<typename T, typename Ts...>
    tuple<Ts...> map(std::function<T (T)> const &f, Ts... args) {
        return make_tuple(static_cast<Ts>(f(static_cast<T>(args)))...);
    }
    

    See, I think you need to cast both the parameter (as a T) and the return type (as a Ts) for the function since it seems some implicit conversion rules are not working inside this template.

    If my syntax doesn’t work (it probably doesn’t, the ...s are tricky when you don’t have a compiler for them), it might be possible that you could rewrite this as a much more verbose function which unpacks each Ts before calling the function, and then builds up a tuple as it goes. I’m not sure if that is really necessary, but my feeling is that compiler support for all of the ... unpacking is a little spotty right now, so even if you come up with something that should work, I wouldn’t be surprised if your compiler couldn’t handle it.

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

Sidebar

Related Questions

I have been playing around with the MVP pattern using winforms for the last
I have been playing around with using graphs to analyze big data. Its been
I have been playing around with modifiers with static method and came across a
I have been playing around with a simple application for Windows Phone 7 using
I have been playing around with using error handling. In particular with user defined
I have been playing around with ElasticSearch for a new project of mine. I
I have been playing around with C++11 lately, and came up with the following
I have been playing around with pointers and function pointers in c/c++. As you
I have been playing around with Google Apps Script today and I am trying
I have been playing around a bit with a fairly simple, home-made search engine,

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.