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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:12:27+00:00 2026-06-16T21:12:27+00:00

In this rather contrived example, I’m trying to pass a function template to my

  • 0

In this rather contrived example, I’m trying to pass a function template to my function, and want my function to instantiate the function template internally. In essence, I don’t want the user to know the types, and workings of my function, but just be able to pass a function template for me to instantiate myself. Self contained example (that doesn’t compile):

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

template <template <typename InputIt, typename OutputIt> class CopyFunc>
void transferWith( CopyFunc<std::vector<int>::const_iterator,
                            std::back_insert_iterator<std::vector<int>>> const& func )
{
    std::vector<int> source{1, 2, 3, 4, 5, 6, 7};
    std::vector<int> sink;

    func(source.begin(), source.end(), std::back_inserter(sink));

    for (auto&& e : sink)
    {
        std::cout << e << std::endl;
    }
}

int main(int argc, char const *argv[])
{

    // I want to pass just the function template in,
    // and instantiate it internally
    transferWith(std::copy);

    return 0;
}

This fails to compile on gcc-4.7.2 as expected, with the following errors:

main.cpp|25 col 27 error| no matching function for call to ‘transferWith(<unresolved overloaded function type>)’
main.cpp|8 col 6 error| note: template<template<class InputIt, class OutputIt> class CopyFunc> void transferWith(const CopyFunc<__gnu_cxx::__normal_iterator<const int*, std::vector<int> >, std::back_insert_iterator<std::vector<int> > >&)
main.cpp|25 col 27 error| note:   couldn't deduce template parameter ‘template<class InputIt, class OutputIt> class CopyFunc’

Are there any tricks or indirections one can pull to get around this?

Thank you.

  • 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-16T21:12:29+00:00Added an answer on June 16, 2026 at 9:12 pm

    Just for completeness, here’s an example of the functor approach:

    #include <algorithm>
    #include <vector>
    #include <iostream>
    
    struct Copy {
      template <typename InputIter, typename OutputIter>
      OutputIter operator()(InputIter i1, InputIter i2, OutputIter o1) {
        return std::copy(i1, i2, o1);
      }
    };
    
    template <typename CopyFunctor> 
    void foo(CopyFunctor cf) {
      std::vector<int> v1 = {3, 1, 4}; 
      std::vector<int> v2(v1.size());
    
      cf(v1.begin(), v1.end(), v2.begin());
    
      for (auto i : v2) {
        std::cout << i << ' ';
      }
    }
    
    int main() {
      foo(Copy());  // displays "3 1 4"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a contrived example of what I want to do, but minimally expresses
Consider this (rather pointless) javascript code: function make_closure() { var x = 123, y
So i have this rather strange issue, i am trying to line up some
I must miss something but I can't see why this contrived example doesn't work:
I have a contrived example in javascript to explain what I am trying to
Here is a rather contrived example of what I am asking: public partial class
Note: I'm more interested in understanding general Go concepts/patterns, rather than solving this contrived
Found this rather strange bug in IE8; element.style.top is limited to 1342177 pixels. Even
I am having this rather long json output in my controller, I wish to
I've been having this rather frustrating problem, and having failed so many times, I'm

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.