小编R.J*_*R.J的帖子

cbegin,在Qt 5.2.1中找出空容器的无效结果

在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都是无效的.这是一个错误或功能吗?

c++ qt c++11

19
推荐指数
2
解决办法
1605
查看次数

Boost asio C++ 20 协程:co_spawn 具有引用参数意外结果的协程

在下面的代码中,会话协程的参数是通过引用传递的。

#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 ServerAGetting 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)

c++ boost-asio c++20 c++-coroutine

3
推荐指数
1
解决办法
1191
查看次数

标签 统计

c++ ×2

boost-asio ×1

c++-coroutine ×1

c++11 ×1

c++20 ×1

qt ×1