for_each中使用的函数中的默认值

Abr*_*ile 1 c++ algorithm boost stl function-pointers

我试着for_eachboost::trim.首先,我使用了错误的代码

 std::for_each(v.begin(),v.end(),&boost::trim<std::string>));
 // error: too few arguments to function
Run Code Online (Sandbox Code Playgroud)

然后我用这个固定(在线阅读)

 std::for_each(v.begin(),v.end()
              ,boost::bind(&boost::trim<std::string>,_1,std::locale()));
Run Code Online (Sandbox Code Playgroud)

当需要将此函数传递给时,编译器如何工作for_each.我认为,因为我的代码std::locale的第二个输入参数的默认参数boost::trim应该有效.

Ker*_* SB 5

调用函数时会应用默认参数,但它们不构成函数签名的一部分.特别是,当您通过函数指针调用函数时,通常会丢失默认参数可用的信息:

void (*f)(int, int);

void foo(int a, int b = 20);
void bar(int a = 10, int = -8);

f = rand() % 2 == 0 ? foo : bar;
f();   // ?
Run Code Online (Sandbox Code Playgroud)

其结果是,使用bindf,你将永远需要填充这两个参数.