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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:28:25+00:00 2026-05-22T23:28:25+00:00

I have a program where I must print many STL vectors on the screen

  • 0

I have a program where I must print many STL vectors on the screen after doing some calculation on each component. So I tried to create a function like this:

template <typename a> 
void printWith(vector<a> foo, a func(a)){
  for_each(foo.begin(), foo.end(), [func](a x){cout << func(x) << " "; });
}

And then use it like this:

int main(){
  vector<int> foo(4,0);
  printWith(foo, [](int x) {return x + 1;});
  return 0;
}

Unfortunately, I’m having a compiling error about the type of the lambda expression I’ve put inside the printWith call:

g++ -std=gnu++0x -Wall -c vectest.cpp -o vectest.o
vectest.cpp: In function ‘int main()’:
vectest.cpp:16:41: error: no matching function for call to ‘printWith(std::vector<int>&, main()::<lambda(int)>)’
vectest.cpp:10:6: note: candidate is: void printWith()
make: *** [vectest.o] Error 1

Of course, if I do:

int sumOne(int x) {return x+1;}

then printWith(foo, sumOne); works as intended. I thought the type of a lambda expression would be the type of a function with the inferred return type. I also though that I could fit a lambda anywhere I could fit a normal function. How do I 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-22T23:28:26+00:00Added an answer on May 22, 2026 at 11:28 pm

    The following works for me:

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    template <typename a, typename F>
    void printWith(vector<a> foo, F f){
      for_each(foo.begin(), foo.end(), [&](a x){cout << f(x) << " "; });
    }
    
    int main(){
      vector<int> foo = {1,2,3,4,5};
      printWith(foo, [](int x) {return x + 1;});
      std::cout << '\n';
      return 0;
    }
    

    Testing:

    $ g++-4.5 -std=gnu++0x -Wall test.cpp
    $ ./a.out                            
    2 3 4 5 6 
    

    Alternatively, you can exploit the fact that closure types with no lambda-capture can be implicitly converted to function pointers. This is closer to your original code and also cuts down on the number of instantiations of the function template (in the original solution you get a new instantiation every time you use the function template with a different function object type; note though that it doesn’t matter much in this specific case since the printWith function is very short and most probably will be always inlined):

    #include <algorithm>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    template <typename a, typename b>
    void printWith(const vector<a>& foo, b f(a)){
      for_each(foo.begin(), foo.end(), [=](a x){cout << f(x) << " "; });
    }
    
    int main(){
      vector<int> foo = {1,2,3,4,5};
      printWith<int, int>(foo, [](int x) {return x + 1;});
      std::cout << '\n';
      return 0;
    }
    

    Unfortunately, implicit conversion doesn’t play very well with template argument deduction: as you can see, I had to specify template arguments in the call to printWith.

    Another alternative is to use std::function. This also helps to minimize the number of template instantiations and works even for lambda expressions with lambda-capture, but has the same problems with template argument deduction:

    #include <algorithm>
    #include <functional>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    template <typename a, typename b>
    void printWith(const vector<a>& foo, std::function<b(a)> f){
      for_each(foo.begin(), foo.end(), [&](a x){cout << f(x) << " "; });
    }
    
    int main(){
      vector<int> foo = {1,2,3,4,5};
      int y = 1;
      printWith<int, int>(foo, [&](int x) { return x + y; });
      std::cout << '\n';
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the need to express simple conditionals as program input. The input must
I have a program that monitors debug messages and I have tried using a
I know this must be a trivial question, but I've tried many different ways,
I got a weird problem. I have a program that (does many things but
I have a program that spits out both standard error and standard out, and
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program that uses the mt19937 random number generator from boost::random. I
I have a program which needs to behave slightly differently on Tiger than on
I have a program that spits out an Excel workbook in Excel 2003 XML
I have a program 'foo' running different threads, fooT1, fooT2, .. fooTn. Now if

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.