Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/132.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++定义了一个宏,它接收函数并传递给其他函数,这是可能的吗?_C++_Function_Macros - Fatal编程技术网

C++定义了一个宏,它接收函数并传递给其他函数,这是可能的吗?

C++定义了一个宏,它接收函数并传递给其他函数,这是可能的吗?,c++,function,macros,C++,Function,Macros,我有这样一个函数: 字符串inputFormattedvoid*errFunc{ 字符串输入; cin>>输入; 如果输入==*errFunc; 返回输入; } 它读取字符串,如果字符串错误,则显示错误 My errFunc的实现方式如下: 无效错误键{ cout您的inputFormatted函数签名必须是: string inputFormatted(void errFunc()) { 为什么要使用宏?只需将GET_PARAM设置为一个函数。那么你的意思是不可能吗?@NathanOlive

我有这样一个函数:

字符串inputFormattedvoid*errFunc{ 字符串输入; cin>>输入; 如果输入==*errFunc; 返回输入; } 它读取字符串,如果字符串错误,则显示错误

My errFunc的实现方式如下:

无效错误键{ cout您的inputFormatted函数签名必须是:

string inputFormatted(void errFunc()) {

为什么要使用宏?只需将GET_PARAM设置为一个函数。那么你的意思是不可能吗?@NathanOliver这不是不可能的,它应该可以工作,我们需要一个函数来告诉你为什么它不工作。这些宏不安全。它们存在于类型系统之外。使用函数可以让代码更安全。谢谢@NathanOliver,我会使用函数,但我只是想使用要知道errFunc参数是否可以使用宏,我想应该将它声明为void*errFunc。如果input==工作正常,尽管我建议如果input.emptyinstead@RemyLebeau-我同意Value*ErrFunc,但这是C方式。在C++中,我们可以简化这个。然后string inputFormattedFunc errFuncI可能只是让函数采用一个模板化的可调用对象:模板string inputFormattedCallable errHandler{string input;cin>>input;if input.empty errHandler;return input;}然后它就不再局限于简单的函数了,它还可以被lambda和functor打开。