使用原始指针,可以这样实现:
#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) 我有一个变种 ScalarVar
using ScalarVar = std::variant<int, std::string>;
Run Code Online (Sandbox Code Playgroud)
和一个变体Var,它本身可能是一个ScalarVar或一个std::vector的ScalarVar小号
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
大约 50% 的时间,对我的线程池的测试不会引发任何异常并且似乎按预期工作。然而,另外 50% 的时间它会抛出一个std::bad_function_call或double 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) 我想编写一个函数,它接受泛型类型的迭代器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) 我有一个需要返回一个数组的函数,因为这是不可能的,而是返回指向该数组的指针.然后我需要在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个元素?
谢谢!
这里有一个如何在堆栈交换上调用 CreateProcess 的示例,但是 Windows 10 似乎不再支持此功能,并且您必须使用 unicode 版本 CreateProcessW。
与 ASCI 版本类似,我正在寻找一个示例: