gpt4 book ai didi

c++ - 我怎样才能避免这段代码中的 void 赋值?

转载 作者:行者123 更新时间:2023-11-30 05:26:00 24 4
gpt4 key购买 nike

我有一个可以为空的 VariantType,即具有 void 状态。

以下代码在使用 Mingw Builds x64 5.3.0 编译时会生成错误:

error: conversion from 'void' to non-scalar type 'VariantType {aka utils::Variant<bool, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >}' requested|

如何避免错误:

#include <Common/Variant.hpp>
using namespace utils;

#include <vector>
#include <unordered_map>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <map>

using VariantType = Variant<bool,int,std::string>;

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

template<typename FuncSig>
class EF : public EFBase
{
public:

EF(std::function<FuncSig> func) : m_function(func) {};

std::function<FuncSig> m_function;
};

class Functions
{
public:

using FuncMap = std::map<std::string,EFBase*>;

FuncMap m_functions;

template<typename FuncType>
void Add(const std::string name, FuncType function)
{
m_functions.emplace(FuncMap::value_type(name,new EF<FuncType>(std::function<FuncType>(function))));
}

template<typename... Args>
VariantType Invoke(const std::string& name, Args... args)
{
auto itr = m_functions.find(name);
if(itr == m_functions.end())
return VariantType();

typedef void Func(typename std::remove_reference<Args>::type...);
std::type_index index(typeid(Func));

const EFBase& a = *itr->second;
std::function<Func> func = static_cast<const EF<Func>&>(a).m_function;
if(typeid(typename std::function<Func>::result_type) == typeid(void))
{
func(std::forward<Args>(args)...);
return VariantType();
}
else
{
VariantType x = func(std::forward<Args>(args)...);
return x;
}
}
};

int main()
{
Functions f;
f.Add<int(bool)>("get3",[](bool x) { return 3; });

VariantType v = f.Invoke<>("get3",true);

return 0;
}

我原以为检查函数对象的 result_type 就足够了;但我想这不是因为模板实例化。我是否需要一种辅助结构来在 void 情况下做一些不同的事情(基于模板参数)?

代码的目的是将任意签名的函数按名称存储在映射中,以便以后调用它们。 VariantType 处理多个可能的返回值。

错误出在 Invoke 方法的 else block 中的赋值。

VariantType 是相当多的代码,所以我没有提供它(我不确定它是否相关)。但如果需要,我可以。

最佳答案

根据具体的用例,一个建议是存储 std::function<VariantType(x)> s 在 map 中(x 是一些固定的参数集),其想法是函数将使用仿函数包装器进行存储以适应特定的签名。客户端函数传递给Add然后 (a) 需要包装在具有正确签名的仿函数中,或者 (b) 如果您知道将传递给 Add 的所有不同类型的函数, 你可以定义 template<typename F, typename ...Args> Add(F f) ,并根据 std::result_of<F(...Args)> 对其进行专门化这样Add可以做 wrapper 。您也可以采用混合方法,要求客户端传递符合固定参数列表和 Add 的函数。可以包装这些函数以返回 VariantType s 基于传入函数的返回类型。

下面的示例展示了一些概念。

请注意 SFINAE 原则应用于 wrapper模板重载以避免编译器在我们不希望它评估的特化中失败(f 返回 void 的那个)。

另请注意,我认为您实际需要在运行时根据函数类型分派(dispatch)不同参数列表的情况可能要困难得多,因此这里的方法试图通过在创建打回来。如果你真的认为你需要 Invoke 中的可变参数列表, 那么我建议也许考虑使用 boost::any包装功能,或至少用于概念指导。

#include <iostream>
#include <type_traits>
#include <functional>
#include <string>
#include <map>
#include <vector>

template<typename T1, typename T2, typename T3>
struct Variant
{
Variant() { std::cout << "In void Ctor of Variant" << std::endl; }
template<typename T> Variant(T t) { std::cout << "In data Ctor of Variant" << std::endl; }
};

using VariantType = Variant<bool,int,std::string>;
using FuncSig = std::function<VariantType(int)>;


struct Functions
{
template<typename F, typename Result = typename std::result_of<F(int)>::type >
void Add(const std::string name, F f)
{
this->m_functions.emplace(name, [=](int i) { return wrapper<F, Result>(f, i); });
}

VariantType Invoke(const std::string& name, int i)
{
auto itr = m_functions.find(name);
if(itr == m_functions.end())
return VariantType();

return itr->second(i);
}
private:
using FuncMap = std::map<std::string,FuncSig>;
FuncMap m_functions;

template<typename F, typename ReturnValue, typename ...Args>
static typename std::enable_if<!std::is_same<ReturnValue, void>::value, VariantType>::type wrapper(F f, Args&&... args)
{
VariantType x = f(std::forward<Args>(args)...);
return x;
}

template<typename F, typename ReturnValue, typename ...Args>
static typename std::enable_if<std::is_same<ReturnValue, void>::value, VariantType>::type wrapper(F f, Args&&... args)
{
f(std::forward<Args>(args)...);
return VariantType();
}
};

struct Client
{
Client(Functions& funcs)
{
funcs.Add("v_func", [&](int i) { this->v_func(this->d, i); } );
funcs.Add("b_func", [&](int i) { return this->b_func(i); } );
funcs.Add("s_func", [&](int i) { return this->s_func(i, this->some_string); } );
funcs.Add("i_func", [&](int i) { return this->i_func(i); } );
}

void v_func(double d, int i) const { std::cout << this->obj_name << ": v_func()" << d << ", " << i << std::endl; }
bool b_func(int i) const { std::cout << this->obj_name << ": b_func()" << i << std::endl; return i > 5; }
std::string s_func(int i, std::string const& s) const { std::cout << this->obj_name << ": s_func()" << i << ", " << s << std::endl; return s; }
int i_func(int i) const { std::cout << this->obj_name << ": i_func()" << i << std::endl; return i + 10; }

std::string obj_name;
const std::string some_string = "some_string";
const double d = 3.14;
};

int main()
{
VariantType variant;
Functions functions;
Client c(functions);
c.obj_name = "Client1";
std::vector<std::string> v = { "s_func", "b_func", "v_func", "i_func" };
int i = 0;
for (auto s : v) { variant = functions.Invoke(s, i++); }

return 0;
}

输出:

In void Ctor of Variant
Client1: s_func()0, some_string
In data Ctor of Variant
Client1: b_func()1
In data Ctor of Variant
Client1: v_func()3.14, 2
In void Ctor of Variant
Client1: i_func()3
In data Ctor of Variant

关于c++ - 我怎样才能避免这段代码中的 void 赋值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38034849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com