小编Blu*_*ue7的帖子

拥有可以指向或引用已在堆栈上分配的不同类型数据的映射的现代方法

使用原始指针,可以这样实现:

#include <iostream>
#include <string>
#include <map>

using namespace std;

class base {
public:
    virtual ~base(){}
};

class derived1 : public base {
public:
    virtual ~derived1(){}
    derived1(const int& other) : val(other) {}
    int val;
};

class derived2 : public base {
public:
    virtual ~derived2(){}
    derived2(const float& other) : val(other) {}
    float val;
};


int main()
{
    derived1 dataA = 4;
    derived2 dataB = 3.0f;

    std::map<std::string, base*> mapOfPointersToData; //Needs to be a pointer because it can be of type deribed1 or …
Run Code Online (Sandbox Code Playgroud)

c++ reference smart-pointers

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

获取一个变体的值,它本身可能是另一个变体

我有一个变种 ScalarVar

using ScalarVar = std::variant<int, std::string>;
Run Code Online (Sandbox Code Playgroud)

和一个变体Var,它本身可能是一个ScalarVar或一个std::vectorScalarVar小号

using Var = std::variant<ScalarVar, std::vector<ScalarVar>>;
Run Code Online (Sandbox Code Playgroud)

我想打一个功能template<typename T, typename Variant> T Get(const Variant& var);是,当给定一个变种,不包括内部变形就只能像std::get<T>,也就是说,它会返回的值T,如果Variant当前包含T,或者如果考虑到包含其他变种的变体,它会递归地获取包含键入直到找到非变体,然后返回。

到目前为止,这是我最好的尝试:

#include <iostream>
#include <variant>
#include <string>
#include <typeindex>
#include <vector>
#include <map>

template<typename T, typename... T2>
struct is_variant { static inline constexpr bool value = false; };

template<typename... T>
struct is_variant<std::variant<T...>> { static inline constexpr bool value = true; }; …
Run Code Online (Sandbox Code Playgroud)

c++ variant algebraic-data-types template-meta-programming c++17

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

为什么我的线程池有时会抛出 `std::bad_function_call` 或 `double free or corruption (!prev)`

大约 50% 的时间,对我的线程池的测试不会引发任何异常并且似乎按预期工作。然而,另外 50% 的时间它会抛出一个std::bad_function_calldouble free or corruption (!prev). 我究竟做错了什么?

#include <thread>
#include <iostream>
#include <atomic>
#include <any>
#include <stack>
#include <mutex>
#include <algorithm>

class reusable_thread {
    std::thread thread;
    std::atomic_bool kill = false;
    std::stack<std::function<void(void)>> function_stack;
    std::stack<std::function<void(void)>> pending_function_stack;
    std::mutex stack_mutex;
    std::atomic_size_t num_jobs = 0;

    /** Seperate containers for functions and pending_function_stack, so that add_job does not have to be locking **/
    inline void transfer_functions() {
        std::lock_guard lock(stack_mutex);
        while(pending_function_stack.size() != 0) {
            function_stack.push(pending_function_stack.top());
            pending_function_stack.pop();
        }
    }

public:

    /** …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading pthreads threadpool c++17

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

函数接受泛型类型 T 的迭代器,并返回仅给出点特定半径内的 T 的迭代器

我想编写一个函数,它接受泛型类型的迭代器T,并返回一个仅给出T点特定半径内的迭代器。因为特征不能包含关联字段,并且我不想为 every实现GetXand ,所以我还将传入一个函数来检索every 的and位置。GetYTxyT

我意识到这不是最有效的空间分区/宽相碰撞检测算法,但我只是想向自己证明具有此签名的函数可以存在于借用检查规则中。

如果这个函数签名无法实现,我该如何修改它以使其工作,并且对T和 的约束/界限最少impl Iterator

这是我的尝试:

pub fn filter<'a, T>(
    world: impl Iterator<Item = &'a T>,
    positions: fn(&'a T) -> (f32, f32),
    near_to: (f32, f32),
    radius: f32,
) -> impl Iterator<Item = &'a T> {
    //We cannot access a field of the geneic item "T" directly, so we instead pass a function that retrieves that field from each "T".
    let positions = …
Run Code Online (Sandbox Code Playgroud)

rust

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

如何使用指针打印数组?

我有一个需要返回一个数组的函数,因为这是不可能的,而是返回指向该数组的指针.然后我需要在main函数中打印这个数组.我该怎么做呢?

这是我的意思的一个例子:

char* myFunction (void)
{

    char myArray[5] = {'one','two','three','four','five'};

return myArray;
}


int main(void)
{

    printf("%s",myFunction);

return 0;

}
Run Code Online (Sandbox Code Playgroud)

但这只是打印指针:uF $.或者,如果我将函数打印为整数,则打印:791013.那么如何实际打印数组中的5个元素?

谢谢!

c c++ arrays pointers

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

如何在 C++ 中调用 unicode 函数 CreateProcessW 来启动 Windows 可执行文件?

这里有一个如何在堆栈交换上调用 CreateProcess 的示例,但是 Windows 10 似乎不再支持此功能,并且您必须使用 unicode 版本 CreateProcessW。

与 ASCI 版本类似,我正在寻找一个示例:

  1. 启动一个 EXE
  2. 等待 EXE 完成。
  3. 可执行文件完成时正确关闭所有句柄。

c++ unicode createprocess

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