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(...)