C 通过参数作为模板
C++ pass function with arguments as template
本文关键字:参数 更新时间:2023-10-16
我在模板上遇到了问题我创建了一个通用类WHCIH存储全局可用信息。该课程拥有一个私人互联克,以管理对全局信息的访问。
template<typename Mutex_Type_T, typename Struct_Type_T>
class CGlobal_Struct
{
public:
/**
* Exports data from this class to target
* @param target the actual target
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
bool Export(Struct_Type_T& target, const uint32_t mutex_timeout = 100);
/**
* Imports data to this class
* @param source The data to store in this class
* @param mutex_timeout Wait Time for Mutex
* @return true in case of success
*/
bool Import(const Struct_Type_T& source, const uint32_t mutex_timeout = 100);
/**
* 1) Loads Data to Buffer
* 2) performs user defined Operation by calling func_T(data, args)
* 3) stores back the data
* @param User defined function
* @param values class data to modify
* @param mutex_timeout Mutex wait time
* @return true in case of success
*/
template<typename func_T, typename func_arg_t>
bool Replace(func_T(Struct_Type_T& values, const func_arg_t args), const func_arg_t func_args,const uint32_t mutex_timeout = 100);
private:
mutex _mutex;
}
此实现看起来像这样
template<typename Mutex_Type_T, typename Struct_Type_T>
template<typename func_T, typename func_arg_t>
bool CGlobal_Struct<Mutex_Type_T, Struct_Type_T>::Replace(func_T(Struct_Type_T& values, const func_arg_t args),const func_arg_t func_args, const uint32_t mutex_timeout)
{
CLock_Guard lock(mutex);
//Lock access
if(false == lock.Lock(mutex_timeout))
{
//Locking failed
return false;
}
//Replace Data
func_T(data, func_args);
//Mutex is released automatically when we leave this function
return true;
}
现在的第一个问题:此模板实现是否正确?
第二:我该如何从此类外部称呼此替换功能?你能给我一些帮助吗?
现在的第一个问题:此模板实现是否正确?
尝试调用它时会编译吗?
第二:我该如何从此类外部称呼此替换功能?
模板参数是从功能指针中推导的。其中 func_T
是返回类型。我会建议反对它,并建议一个更简单的模板参数:
template<typename Struct_Type_T>
struct CGlobal_Struct
{
template<typename func_T, typename func_arg_t>
bool Replace(func_T function, const func_arg_t func_args,const uint32_t mutex_timeout = 100) {
// function(struct_type_value, func_args);
}
};
两个版本都是有效的,被称为这样:
struct A {};
void func(A&, int) {}
int main() {
CGlobal_Struct<A> glob;
glob.Replace(func, 1);
}
我推荐的版本也可以支持lambdas:
glob.Replace([](A&, int){ /* code */ }, 1);
阅读您的实现,它无法与您当前版本一起使用:
//Replace Data
func_T(data, func_args);
func_T
是发送为参数的函数的返回类型的类型。与此相同:
void(data, func_args);
或更多邪恶:
struct evil { evil(A&, int){} };
// ...
evil(data, func_args);
这将调用evil
的构造函数,永远不要调用函数。
您仔细观察,您的参数没有名称:
bool Replace(
func_T(Struct_Type_T&, const func_arg_t),
/* other params */ );
给它一个名字,Sytax将是:
bool Replace(
func_T(*function)(Struct_Type_T&, const func_arg_t),
/* other params */ );
然后致电function(...)
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用