小编joe*_*ech的帖子

Why is iterating over std::ranges::views::join so slow

This is a follow-up of this SO Answer. Given a flat input range and three size_t dimensions, the code creates a nested random_access_range of random_access_ranges of random_access_ranges, modelling a three-dimensional array.

在此输入图像描述 Quickbench

Iterating over the elements in the "multidimensional" range using a nested-for loop and indices is a bit slower than directly iterating over the elements of the input range (factor 4 slower). I suppose some performance drop can be expected, but a factor of 4 hurts …

c++ benchmarking c++20 std-ranges

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

对于简单的 std::ranges 代码,某些编译器存在错误或编译错误

我有一段使用 C++20 范围库的代码,取自此 SO anwer。该代码被某些编译器(版本)拒绝,并且某些较旧的 GCC 版本返回垃圾。哪个编译器是正确的?

该代码应该打印 a 中第一列的元素std::vector<std::vector>

#include <vector>
#include <string>
#include <ranges>
#include <iostream>

int main()
{
    // returns a range containing only the i-th element of an iterable container
    auto ith_element = [](size_t i) {
        // drop the first i elements in the range and take the first element from the remaining range
        return std::views::drop(i) | std::views::take(1);
    };

    // returns a range over the i-th column
    auto column = [ith_element](size_t i) …
Run Code Online (Sandbox Code Playgroud)

c++ c++20 std-ranges

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

通过插件接口公开可变参数函数

我有一个框架,它有一个类型擦除的函数类型Function,将std::any实例映射到std::any实例和函数注册表std::unordered_map<std::string, Function>。该框架有一个插件系统,插件作者可以在此函数注册表中注册函数。这样,新功能可以在运行时集成到框架中。

该框架的一个非常精简的实现可能如下所示:

#include <any>
#include <vector>
#include <memory>
#include <functional>
#include <iostream>

class Function
{
public:

    template <typename R, typename... Args>
    Function(R(*f)(Args...)) : impl(std::make_unique<FunctionImpl<R, Args...>>(f)) {}


    template <typename... Args>
    std::any operator()(Args&&...args) const
    {
        std::vector<std::any> args_vec({std::forward<Args>(args)...});
        return impl->invoke(args_vec);
    }

private:

    struct FunctionProxy {
        virtual std::any invoke(std::vector<std::any>&) const = 0;
        virtual ~FunctionProxy(){}
    };

    template <typename R, typename... Args>
    class FunctionImpl : public FunctionProxy
    {
    public:
        FunctionImpl(R(*f)(Args...)) : fun{f} {}

        std::any invoke(std::vector<std::any>& args) const …
Run Code Online (Sandbox Code Playgroud)

c++ plugins functional-programming variadic-templates c++17

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

通过 std::any 向上转换

在 C++ 中,如果是 的基类,则可以将 的实例传递Derived给接受 的函数。这对于满足 API 非常有用,也是 API 的一种非常常见的设计模式。BaseBaseDerived

目前我面临着一种情况,我想通过std::any. 也就是说,我有std::any一个存储 的实例Derived,并且我想将其地址转换为BaseAPI 函数中的指针,该函数不应该知道 的存在Derived。我的用例是一个运行时反射库,它传递std::any存储反射类型的实例。

我知道向上转换std::any是不可能的,因为std::any_cast在转换之前检查typeid并返回 anullptr如果类型不匹配,请参阅cppreference

但也许有一个解决方法或一些我可以使用的聪明技巧?感觉这一定是可能的,因为向上转换在 C++ 中是很常见的事情,并且std::any 已经需要的地址和类型。

这是一个代码示例。以下代码按预期出现段错误,因为std::any_cast返回的 a在下一行中被取消引用nullptrany_function编译器浏览器:https://godbolt.org/z/E9sG9G3ff

#include <any>
#include <iostream>

class Base 
{
public:
    double get_val() const {
        return val;
    }

private: 
    double val {1.23};
};

void …
Run Code Online (Sandbox Code Playgroud)

c++ std c++17 stdany

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