c++ - 将函数作为模板参数传递给类
问题描述
我想将函数作为模板参数传递,但不知何故我无法让它工作。我过去写过很多模板类,但只使用“普通”类型作为参数。我已经搜索过这个问题,但似乎没有什么适合这里。所以我被困住了。
这是我想要实现的目标:我有几个要封装的特定于类型的库函数。我知道还有其他方法可以做到这一点(例如继承),但由于我也想在这里学习一点,我想用模板方法完成它。
我想要达到的目标:
MyType<int> myIntObject(...);
...
int writeVal = 10;
myIntObject.Write(writeVal);
...
int readVal;
myIntObject.Read(&readVal);
也许还使用 typedefs:
typedef MyType<int> MyIntType;
到目前为止我做了什么:
template<typename T>
struct SetFunction
{
inline ret_code operator()(someArgs, T value) const {return E_Fail;}
}
template<typename T>
struct GetFunction
{
inline ret_code operator()(someArgs, T& value) const {return E_Fail;}
}
//template specialization:
template<>
struct SetFunction<int>
{
inline ret_code operator()(someArgs, T value) const
{
//some other code
return libraryFunctionWriteInt(someArgs, value);
}
}
template<>
struct GetFunction<int>
{
inline ret_code operator()(someArgs, T& value) const
{
//some other code
return libraryFunctionReadInt(someArgs, &value);
}
}
//do the specialization for all other types as well
template<class T, typename T_get_function = GetFunction<T>(), typename T_set_function = SetFunction<T>()>
MyType
{
public:
MyType(someArgs)
{
if(T_get_function(someArgs, &_value) == E_Fail)
{
throw someting;
}
}
bool SetValue(T value)
{
_value = value;
//maybe some other code
return T_set_function(someArgs, _value) == E_Success;
}
bool GetValue(T &value)
{
//maybe some other code
return T_get_function(someArgs, &_value) == E_Success;
}
private:
T _value;
}
//and then eventually using it (maybe using some typedefs as well)
MyType<int> myIntObject(someArgs);
...
int writeVal = 10;
myIntObject.SetValue(writeValue);
...
int readVal;
myIntObject.GetValue(&readValue);
现在我得到编译错误:在我调用T_set_function / T_get_function
. 我还尝试将库函数直接作为模板参数传递,并且不设置任何默认函数,但我也无法让它以这种方式工作。
到目前为止,我阅读的几乎所有书籍、文章和示例都只处理将“普通”类型作为模板参数传递,但由于 stl 也使用传递函数,我真的很想知道如何做到这一点。
解决方案
关于运算符的参数有一些不完整的代码,但语法
template<class T, typename T_get_function = GetFunction<T>(),
typename T_set_function = SetFunction<T>()>
是不正确的。typename T_get_function =
建议模板的类型参数,而在 = 之后是一个表达式GetFunction<T>()
,而不是一个声明符。
它应该是
template<class T, typename T_get_function = GetFunction<T>,
typename T_set_function = SetFunction<T>>
然后不清楚您希望如何在T_get_function::operator()
没有类实例的情况下工作。它要么应该是静态的,要么你应该有一个实例。
template<class T, T_get_function getter = GetFunction<T>{},
T_set_function setter = SetFunction<T>>{}
在这种特殊情况下typename
,class
意思相同,在那里不需要。但我们不知道那T_get_function
是什么?将其作为模板的类参数并分配默认类是 C++20 特性。
运算符 & 返回一个指向变量的指针,您不能将其用作参考(并且您不需要这样做)。该元代码包含几十个问题,从缺少;语法不正确以及缺少关键字和声明。如果您在 2020 年之前专注于 C++ 标准,则应该从头开始重写,否则请使用 Klaus 的答案作为指导。
推荐阅读
- ruby-on-rails - 如何在三星 Dex 内的 termux 上运行 rails 服务器?
- redux - TypeError :state.foo 未定义
- swift - SwiftUI Navigation 类似于 Slack
- linux - jackd 无法在 docker 容器中使用实时调度
- java - 使用可比较的类保留 2D Arraylist 的原始索引
- mysql - 如何在没有原始选择的情况下在 Laravel 中绑定 sql 查询参数?
- spring-boot - RedisCache 类中的 NullPointerException(行号 614)。版本 1.8.22
- python - Receiving KeyError: ' ' for my text based rpg
- java - JSONArray 与 Array 或 Arraylist
- android - 'React native run android' 在模拟器中启动应用程序后停止