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

  • Home
  • SEARCH
  • 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 6114681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:00:12+00:00 2026-05-23T15:00:12+00:00

I stumbled on a strange compilation problem. I want to process a list of

  • 0

I stumbled on a strange compilation problem. I want to process a list of strings, using std::for_each. The following simplified code illustrates the problem :

# include <list>
# include <string>
# include <algorithm>

using namespace std ;

void f(wstring & str)
{
    // process str here
}

void g(wstring & str, int dummy = 0)
{
    // process str here, same as f, just added a second default dummy argument
}

int main(int, char*[])
{
    list<wstring> text ;

    text.push_back(L"foo") ;
    text.push_back(L"bar") ;

    for_each(text.begin(), text.end(), f) ;  // OK, fine :)
    for_each(text.begin(), text.end(), g) ;  // Compilation error, complains about 
                     // g taking 2 arguments, but called within std::for_each
                     // with only one argument. 

    // ...
    return 0 ;
}    

I tested using MinGW 4.5.2 and MSVC10, both reported the same error message. Originally, I wanted to use boost::algorithm::trim as a processing function passed to std::for_each, but I found that it takes two arguments, the first being mandatory (the string to process) and the second one is optional (a locale providing a definition for space chars).

Is there any way to keep things clean when using std::for_each(and other standard algorithms) when having functions or methods with default arguments ? I found a way to make it work, but it is no more clear and easily understandable, so a for loop begins to seem easier …

# include <list>    
# include <string>
# include <algorithm>
# include <boost/bind.hpp>
# include <boost/algorithm/string.hpp>

using namespace std ;
using namespace boost ;

// ... somewhere inside main
list<wstring> text ;
for_each(text.begin(), text.end(), bind(algorithm::trim<wstring>, _1, locale()) ;
// One must deal with default arguments ...
// for_each(text.begin(), text.end(), algorithm::trim<wstring>) would be a real pleasure

Thanks for any help !

Note : I just started learning English, sorry for mistakes 🙂

  • 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-23T15:00:13+00:00Added an answer on May 23, 2026 at 3:00 pm

    Default arguments are just a code-generation tool and not part of the function signature, so you can’t really get around that. You could wrap your function in a function object, but that’s precisely what bind already does for you.

    However, in C++0x you can conveniently store the result (and use std::bind) to maybe make the code a bit more readable:

    auto trimmer = std::bind(boost::algorithm::trim<std::wstring>, std::placeholders::_1, std::locale());
    
    std::for_each(text.begin(), text.end(), trimmer);
    

    (The reason you don’t want to do that in C++98/03 is that the return type of bind is something rather unsightly, and you wouldn’t be doing anyone a favour by spelling it out.)

    Alternatively, again in C++0x, you could use a lambda:

    std::for_each(text.begin(), text.end(), [](std::wstring & s){ boost::algorithm::trim<std::wstring>(s); });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've stumbled onto a very strange bug. Read the comments in the code to
I stumbled across this problem in F#. Suppose, I want to declare two types
I stumbled upon a very strange bit of PHP code. Could someone explain why
I stumbled about a strange problem with ListViews. In the emulator the ListView shows
I've stumbled upon a strange problem. I made a regular os x system update
In a code review today, I stumbled across the following bit of code (slightly
Doing some GIS programming I stumbled over the following strange issue: When I apply
I stumbled on a strange problem when trying to port a piece of C++
I stumbled on the same problem of nested templates described here . The following
I stumbled across this code and am too proud to go and ask the

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.