在Qt 5.2.1中,以下代码结果如何不同?
QVector<int> c;
if (c.cbegin() != c.begin())
{
std::cout << "Argh!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这打印"argh",但以下没有.
QVector<int> c;
if (c.begin() != c.cbegin())
{
std::cout << "Argh!" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
请注意,cbegin和begin位置已切换.但是如果你更改容器状态我的意思是例如push_back中的东西,它可以正常工作.在我调用容器上的任何可变方法之前,cbegin和cend都是无效的.这是一个错误或功能吗?
在下面的代码中,会话协程的参数是通过引用传递的。
#include <boost/asio.hpp>
#include <iostream>
boost::asio::awaitable<void> session(const std::string& name)
{
std::cout << "Starting " << name << std::endl;
auto executor = co_await boost::asio::this_coro::executor;
}
int main()
{
boost::asio::io_context io_context;
co_spawn(io_context, session("ServerA"), boost::asio::detached);
co_spawn(io_context, session("ServerB"), boost::asio::detached);
io_context.run();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于某种我不明白的原因,上面的代码会导致打印Starting ServerB两次。
> g++ -std=c++20 ../test-coro.cpp -o test-coro && ./test-coro
Starting ServerB
Starting ServerB
Run Code Online (Sandbox Code Playgroud)
但是当我将协程参数更改为按值传递时,它将正确打印Starting ServerA和Getting ServerB
#include <boost/asio.hpp>
#include <iostream>
boost::asio::awaitable<void> session(std::string name)
{
std::cout << "Starting " << name << std::endl;
auto executor = …Run Code Online (Sandbox Code Playgroud)