【发布时间】:2020-05-27 22:26:44
【问题描述】:
我有一个包含 lambda 函数的类,如下所示:
class Foo {
private:
inline static std::string text{};
public:
template<typename...T>
inline static auto func = [](T&&...args) mutable throw() {
text += std::string(args...);
};
void show() {
std::cout << text << '\n';
}
};
我的预期用途是这样的:
int main() {
Foo bar;
bar.func<std::string, int, std::string>( "Hello, I am", 39, "years old!");
bar.show();
return 0;
}
我希望模板化的可变参数 lambda 接收任何类型的基本类型参数,例如 string、char*、char[]、int、float、double 等。 . 并将它们全部转换为单个 std::string 将存储在类中。
当我这样运行我的代码时:
int main() {
Foo bar;
bar.func<string>( "Hello world!");
bar.show();
return 0;
}
一切都编译得很好,但是,当我开始添加各种类型时,例如上面预期用途的示例,它无法编译。 Microsoft Visual Studio 给了我一个C2400 编译器错误:无法从初始化列表转换为 std::string。没有构造函数可以采用源类型,或者构造函数重载决议不明确......
我相信我理解为什么它会模棱两可,因为这不是问题所在。我的问题是使用“移动语义或完美转发”的正确有效方式是什么?我试图避免一堆临时副本。
【问题讨论】:
-
拜托,您能否添加一个导致编译器错误的示例(调用)?
-
@max66 预期用途是产生错误的用途。
标签: string lambda c++17 move variadic