提问人:Sami 提问时间:7/16/2023 最后编辑:Remy LebeauSami 更新时间:7/17/2023 访问量:190
为什么在 C++ 中使用 std::thread 时需要为字符串参数传递 std::span?
Why do I need to pass std::span for a string argument when using std::thread in C++?
问:
我编写了以下代码,该代码创建了一个重复打印字符串的线程。
在此代码中,我可以直接将字符串作为参数传递给函数中的函数。但是,当我想创建一个调用该函数的线程时,我必须将字符串传递到 .repeat()
main()
repeat()
std::span
为什么会这样?为什么需要字符串参数,而函数本身不需要?std::thread
std::span
repeat()
错误:
Error C2780: 'std::invoke' expects 1 arguments, 3 provided.
Error C2893: Failed to specialize function template 'std::invoke'.
Visual Studio 2019 企业版。C++20
#include <span>
#include <iostream>
void repeat1(std::span<const char> str, int n)
{
if (n > 0)
{
std::cout << str.data() << std::endl;
repeat1(str, n - 1);
}
}
void repeat2(std::string_view str, int n)
{
if (n > 0)
{
std::cout << str.data() << std::endl;
repeat2(str, n - 1);
}
}
int main()
{
repeat1("I am exploring...", 3); //it works
repeat2("I am exploring...", 3); //it works
//std::thread t(repeat2, "I am exploring...", 5); // It works
//std::thread t(repeat1, std::span < const char>("I am exploring..."), 5); // It works
std::thread t(repeat1, "I am exploring...", 5); // It doesn't compile
std::cout << "Hello from main()\n";
t.join();
}
答:
4赞
康桓瑋
7/16/2023
#1
std::thread
在调用之前将参数复制到衰减类型。
在您的示例中,字符串文字衰减为 ,它不再是一个范围,因此不能用于构造 .const char*
std::span
评论
std::span
std::thread t(repeat, str, 5);
repeat(str, 5)