0

我正在尝试使用 lambdas 在我的函数参数中组合多个步骤。我努力了:

void testLambda(const char* input, const char* output = [](const char* word){return word;}(input)){

     std::cout << input << " " << output << std::endl;

}

这个函数应该:如果

testLambda("hallo");

被调用,从第一个参数中获取并创建第二个参数(默认)并打印出来hallo hallo。我怎样才能使这项工作?

4

1 回答 1

2

你不能这样做——默认参数不够复杂。即使是这样,这也不是非常清晰的代码。

只写一个重载!

void testLambda(const char* input, const char* output)
{
     std::cout << input << ' ' << output << '\n';
}

void testLambda(const char* input)
{
    return testLambda(input, input);
}

或者,如果您不想这样做:

void testLambda(const char* input, const char* output = nullptr)
{
     std::cout << input << ' ' << (output ? output : input) << '\n';
}

(然后重命名函数:P)

没有必要让这个模式变得复杂。

于 2019-01-18T12:41:07.380 回答