小编bob*_*eff的帖子

从另一个线程恢复asio协同程序

我有从另一个线程恢复boost :: asio coroutine 的问题.这是示例代码:

#include <iostream>
#include <thread>

#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/spawn.hpp>

using namespace std;
using namespace boost;

void foo(asio::steady_timer& timer, asio::yield_context yield)
{
    cout << "Enter foo" << endl;
    timer.expires_from_now(asio::steady_timer::clock_type::duration::max());
    timer.async_wait(yield);
    cout << "Leave foo" << endl;
}

void bar(asio::steady_timer& timer)
{
    cout << "Enter bar" << endl;
    sleep(1); // wait a little for asio::io_service::run to be executed
    timer.cancel();
    cout << "Leave bar" << endl;
}

int main()
{
    asio::io_service ioService;
    asio::steady_timer timer(ioService);

    asio::spawn(ioService, bind(foo, …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading boost-asio

4
推荐指数
1
解决办法
1537
查看次数

如何为boost :: log console输出添加颜色编码?

我正在尝试为linux下的boost :: log添加彩色日志输出.我读了以下内容,并尝试了这个:

#define MY_LOG_ERROR() BOOST_LOG_TRIVIAL(error) << "\033[1;31"

MY_LOG_ERROR() << "This is an error log."
Run Code Online (Sandbox Code Playgroud)

但它给了我以下结果:

[2016-07-11 17:23:16.328435] [0x00007f15f03d6780] [错误] [1; 31这是一个错误日志.

如何正确添加彩色日志输出到boost :: log?

c++ logging boost boost-logging boost-log

4
推荐指数
1
解决办法
3043
查看次数

如何在 C++ 中定义编译时三元文字?

《C++ 编程语言》书第 4版的第 19 章中,有一个使用模板技术定义三进制数字文字的示例,但该示例无法编译。我尝试以我认为正确的方式修复它,但它仍然无法编译。

#include <cstdint>
#include <iostream>

using namespace std;

constexpr uint64_t ipow(uint64_t x, uint64_t n)
{
  return n > 0 ? x * ipow(x, n - 1) : 1;
}

template <char c>
constexpr uint64_t base3()
{
  static_assert(c >= '0' && c <= '2', "Not a ternary digit.");
  return c - '0';
}

template <char c, char... tail>
constexpr uint64_t base3()
{
  static_assert(c >= '0' && c <= '2', "Not a ternary …
Run Code Online (Sandbox Code Playgroud)

c++ templates user-defined-literals variadic-templates c++11

4
推荐指数
1
解决办法
77
查看次数

如何消除“erase_all_if”函数模板重载的歧义?

我有两个erase_all_if函数模板的实现。

template <typename Container, typename Pred>
typename Container::size_type erase_all_if(Container& c, Pred&& pred)
{
    auto newend = std::remove_if(c.begin(), c.end(), std::forward<Pred>(pred));
    auto ret = c.end() - newend;
    c.erase(newend, c.end());
    return ret;
}

template <typename Container, typename Pred>
auto erase_all_if(Container& c, Pred&& pred) {
    typename Container::size_type removed = 0;
    for (auto it = c.begin(); it != c.end();) {
        if (pred(*it)) {
            it = c.erase(it);
            ++removed;
        }
        else {
            ++it;
        }
    }
    return removed;
}

Run Code Online (Sandbox Code Playgroud)

第一个仅适用于容器,std::vector因为它需要随机访问迭代器,第二个适用于所有容器,但它对于连续容器效率较低,并且适用于基于节点的容器。如何消除两个版本的歧义?我当前使用的 C++ 标准是 C++17。

c++ templates stl sfinae c++17

4
推荐指数
2
解决办法
74
查看次数

通过 Git 远程跟踪分支的给定名称如何找到哪个本地分支跟踪它?

例如,通过给定一个 Git 远程跟踪分支的名称,upstream/develop如果有的话,如何找到哪个本地分支跟踪它?

如果可能,我正在寻找一种不依赖于 shell 脚本并且也适用于 Windows 的解决方案。

git version-control

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

如何检查某个遥控器上是否存在 Git 提交?

我想检查特定遥控器上是否存在特定的 Git 提交 sha1。我尝试了以下命令:

git fetch <remote> <revision> -q --dry-run
Run Code Online (Sandbox Code Playgroud)

问题是该命令不仅在远程存在修订时返回成功,而且在本地存在时也返回成功。

有什么方法可以忽略本地版本来检查这个吗?

git version-control

2
推荐指数
1
解决办法
76
查看次数

Rust 智能指针 std::rc::Rc 和 std::sync::Arc 分别类似于 C++ 智能指针 std::shared_ptr 和 std::atomic_shared_ptr 吗?

锈智能指针之间存在类比std::rc::Rc,并std::sync::Arc与C ++智能指针std::shared_ptrstd::atomic_shared_ptr?对我来说,它们看起来一样,但可能有一些实现上的细微差别。例如在 C++std::shared_ptr中,控制块中的引用计数是原子的,尽管指针本身不是。在 Rust 中是一样的std::rc::Rc吗?

c++ smart-pointers rust

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