小编Mai*_*kel的帖子

使用gsl :: span和range-v3

我尝试了一个小例子来熟悉GSL和range-v3库,我想知道它们如何协同工作.我有这个玩具的例子

#include <iostream>
#include <range/v3/all.hpp>

using namespace std;
using namespace ranges;

void example_vector(vector<int> const& v)
{
  ranges::for_each(view::tail(v), [](int x){
    cout << x << ' ';
  });
  cout << '\n';
}

int main()
{
   auto seq = vector<int> { 2,2,2,0,0,2,1,2 };
   example_vector(seq);
}
Run Code Online (Sandbox Code Playgroud)

哪个有效.但是,如果我尝试使用gsl::span<int>范围,则会导致错误消息.编译器告诉我span没有满足视图概念.

#include <gsl.h>

// ...

void example_span(gsl::span<const int> v)
{
  ranges::for_each(view::tail(v), [](int x){
    cout << x << ' ';
  });
  cout << '\n';
}
Run Code Online (Sandbox Code Playgroud)

编译器消息:

note: candidate template ignored: disabled by 'enable_if'
      [with Rng …
Run Code Online (Sandbox Code Playgroud)

c++ range-v3 guideline-support-library

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

如何正确转发Invocable类型

我真的很喜欢使用cmcstl2,这是Ranges TS的一个实现.我特别喜欢每个STL算法的可选投影.Invocable类型转发(呃......或不)像这样:(min_element.hpp)

template <ForwardIterator I, Sentinel<I> S,
    class Comp = less<>, class Proj = identity>
requires
    IndirectStrictWeakOrder<
        Comp, projected<I, Proj>>()
I min_element(I first, S last, Comp comp = Comp{}, Proj proj = Proj{});

template <ForwardRange Rng, class Comp = less<>, class Proj = identity>
requires
    IndirectStrictWeakOrder<
        Comp, projected<iterator_t<Rng>, Proj>>()
safe_iterator_t<Rng>
min_element(Rng&& rng, Comp comp = Comp{}, Proj proj = Proj{})
{
    return __stl2::min_element(__stl2::begin(rng), __stl2::end(rng),
        __stl2::ref(comp), __stl2::ref(proj));
}
Run Code Online (Sandbox Code Playgroud)

作为参考:range-v3库实现如下:(min_element.hpp)

struct min_element_fn …
Run Code Online (Sandbox Code Playgroud)

c++ perfect-forwarding c++-concepts range-v3 c++17

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

How to debug code with std::optional or std::variant?

我使用libc ++,libstdc ++都没关系,无论gdb还是lldb都无法可视化std::optionalstd::variant。我已经用谷歌搜索了一下,但是没有遇到明显的解决方案。

我错过任何漂亮的打印机吗?

c++ gdb pretty-print lldb c++17

5
推荐指数
0
解决办法
540
查看次数

`std::future::then` 会分配内存吗?可以自定义分配器吗?

std::promise有一个构造函数,它分配用于存储共享状态的内存。.then在我到目前为止所看到的 的实现中,人们以类似于共享状态std::function 的类型擦除方式存储函数。stlab 的并发库甚至放置了一个

using then_t = std::vector<std::pair<executor_t, task<void()>>>;
Run Code Online (Sandbox Code Playgroud)

进入共享状态(以启用可拆分的 future)。

是否有意指定是否可以为延续指定分配器?

P0443R3中,他们在 1.2.9 自定义内存分配的属性中说

执行器实现应使用提供的分配器来分配存储提交的函数对象所需的任何内存。

我认为这仅意味着通过函数提交的代理的存储,而不是共享状态中的execute指针。 std::future

我研究了最近关于期货或执行人的提案,但我找不到任何东西。无论如何,最近的论文似乎倾向于让执行者处于共同的未来状态。如果执行器有一个关联的自定义分配器,是否应该使用它?

我感觉我错过了什么。

--

编辑:我对此进行了更多思考,我想我可以理解为什么有人愿意不明确这一点。如果 future 由执行者参数化,那么它们的共享状态可能如下所示

template <typename Executor, typename T>
struct shared_state_t {
    // some prevention from race condition, for example
    std::atomic<unsigned> flags;
    // some sort of exception or value representation, for example
    std::variant<std::exception_ptr, T> maybe_value;
    // some handle to continuation
    continuation_t<Executor, T> continuation;
};
Run Code Online (Sandbox Code Playgroud)

wherecontinuation_t<Executor, …

c++ future executor

5
推荐指数
0
解决办法
315
查看次数

如何使用`std :: basic_istream <std :: byte>`之类的东西

此问题旨在使用std::byte标准输入输出.

是否有计划增加适当的功能重载read(_bytes)write(_bytes)到的接口basic_istream<CharT>,并basic_ostream<CharT>在将来的标准是什么?有什么理由反对呢?我知道CharT*应该保留-overloads.我该怎么办std::byte?我目前在我的项目功能中定义

std::istream& read(std::istream&, std::byte*, std::streamsize)
std::ostream& write(std::ostream&, const std::byte*, std::streamsize)
Run Code Online (Sandbox Code Playgroud)

这些使用reinterpret_cast<>char*RESP.const char*但我相信这取决于它的大小char.我错了吗?是char永远1 byte

我试图制作,std::basic_istream<std::byte>但它缺少std::char_traits<std::byte>等等.有人做过这种事吗?

c++ io std c++17

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

cppreference上错误的std :: condition_variable示例?

在他们的示例用法中,std::condition_variable他们基本上有

std::mutex m;
std::condition_variable cv;
bool ready = false;

void worker_thread()
{
    // Wait until main() sends data
    std::unique_lock<std::mutex> lk(m);
    cv.wait(lk, []{return ready;});
    // more ...
}

int main()
{
    std::thread worker(worker_thread);

    data = "Example data";
    // send data to the worker thread
    {
        std::lock_guard<std::mutex> lk(m);
        ready = true;
    }
    cv.notify_one();
    // more...
}
Run Code Online (Sandbox Code Playgroud)

我现在的问题ready是未声明的变量std::atomic*.

如果发生虚假唤醒,为什么不引入竞争条件?

c++ multithreading race-condition c++11

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