C++ 神秘的C++;语法

C++ 神秘的C++;语法,c++,C++,我试着去了解一个卡方程序,它使用内在的速度。在这个过程中,我遇到了一行我无法理解的代码 我试过复习课本,谷歌和搜索这个网站,但都没有成功。我认为问题在于,在不了解任何语法的情况下,我无法用术语或关键字充分描述搜索,从而获得任何相关结果 这是我不明白的一行代码: float (*chi2_float)(const int, const float*, const float*) = chi2_baseline_float; 下面是包含它的函数: float chi2_float(const in

我试着去了解一个卡方程序,它使用内在的速度。在这个过程中,我遇到了一行我无法理解的代码

我试过复习课本,谷歌和搜索这个网站,但都没有成功。我认为问题在于,在不了解任何语法的情况下,我无法用术语或关键字充分描述搜索,从而获得任何相关结果

这是我不明白的一行代码:

float (*chi2_float)(const int, const float*, const float*) = chi2_baseline_float;
下面是包含它的函数:

float chi2_float(const int dim, const float* const x, const float* const y) {
    float (*chi2_float)(const int, const float*, const float*) = chi2_baseline_float;
#ifdef __SSE__
    chi2_float = chi2_intrinsic_float;
#endif
    return chi2_float(dim, x, y);
}
在我看来,它可能是在声明和定义一个函数,当我注释掉该行并重新编译时,我得到:

错误C2659:“=”:函数作为左操作数 在线 chi2_浮点=chi2_固有_浮点

如果需要,我可以发送包含此函数的.h文件,但这与您从参数中所期望的一样


任何帮助都将不胜感激。

该行正在设置一个类型为函数指针的变量,该变量指向其他两个函数之一,具体取决于
\uuu SSE\uu
的值

然后它调用chi2_float指向的函数并返回结果。

这是:

float (*chi2_float)(const int, const float*, const float*)= chi2_baseline_float;
声明函数指针名称
chi2\u float
,并为其分配一个指向名为
chi\u baseline\u float
的函数的指针

然后,如果定义了
\uuuu SSE\uuuu
宏,则将使用指向函数
chi2\u intrinsive\u float
的指针重新分配指针

所有这些的净影响类似于:

float chi2_float(const int dim, const float* const x, const float* const y) 
{
#ifdef __SSE__
    return chi2_intrinsic_float(dim, x, y);
#else
    return chi2_baseline_float(dim, x, y);
#endif
}

您的问题代码和一些注释:

// beginning of the definition of function chi2_float 
float chi2_float(const int dim, const float* const x, const float* const y) {
    // declare the variable chi2_float as a function pointer
    // set variable chi2_float to the address of the function chi2_baseline_float
    float (*chi2_float)(const int, const float*, const float*) = chi2_baseline_float;
// if macro __SSE__ is defined (if the compiler enables SSE instructions set)
// [this is your case because you got an error in the below line when you have commented the above line] 
#ifdef __SSE__
    // then preprocessor adds the following line that sets again the variable chi2_float (but to another function) 
    chi2_float = chi2_intrinsic_float;
#endif
    // call the function pointed by the variable chi2_float
    return chi2_float(dim, x, y);
}
丑陋的

要修复此代码,首先要使用函数名以外的其他名称作为函数指针变量的名称。这个变量隐藏了函数的名称,我认为这是James Crow困惑的根源


修复此代码所要做的第二件事是完全删除函数指针,从而产生Michael Burr在其答案中发布的代码。

Michael Burr建议如下:

float chi2_float(const int dim, const float* const x, const float* const y) 
{
#ifdef __SSE__
    return chi2_intrinsic_float(dim, x, y);
#else
    return chi2_baseline_float(dim, x, y);
#endif
}
#ifdef __SSE__
    #define chi2_float(dim, x, y) chi2_intrinsic_float(dim, x, y)
#else
    #define chi2_float(dim, x, y) chi2_baseline_float(dim, x, y)
#endif
暂时忽略编译器的优化程序;这是可以改进的。Burr先生的解决方案使用两个函数调用:main()(或其他)调用chi2_float(),然后调用相应的实现。这可以通过以下方式减少为一个函数调用:

float chi2_float(const int dim, const float* const x, const float* const y) 
{
#ifdef __SSE__
    return chi2_intrinsic_float(dim, x, y);
#else
    return chi2_baseline_float(dim, x, y);
#endif
}
#ifdef __SSE__
    #define chi2_float(dim, x, y) chi2_intrinsic_float(dim, x, y)
#else
    #define chi2_float(dim, x, y) chi2_baseline_float(dim, x, y)
#endif
同样的结果也可以通过将Burr先生的chi2_float()声明为“inline”来实现

然而,回到现实世界(编译器积极优化代码),你会期望一个好的优化者删除额外的函数调用,从而使Burr先生的解决方案同样快速


为了完整起见,我已经发布了此讨论。虽然这并没有改善伯尔先生的解决方案,但它增加了一点背景。从技术上讲,它应该是一个注释,而不是一个新的答案,但不可能在注释中格式化源代码。生活就是这样。

不那么神秘;它只是一个函数指针。你的C++教材很方便吗?相似,但不一样。不幸的是,问题中的代码将没有那么有效,因为调用是通过函数指针进行的,这不仅增加了间接级别,而且阻止了任何可能的内联优化。您比较过的代码应该更快,如果可能的话,我建议OP用这个示例替换他的代码。谢谢Michael Burr。我接受你的建议,重新编写代码。它更清晰易懂。谢谢。现在我知道它是一个类型函数指针,我明白了。谢谢David Hammen。你是对的,我认为函数名和函数指针变量名相同增加了我的困惑。谢谢Michael J的回答。我正在按照迈克尔·伯尔的建议修改密码。我也可能会将代码简化为一个函数调用,但我需要首先更深入地理解整体逻辑。感谢olibre,您的评论非常具有说明性,使“神秘”的语法更容易理解。欢迎@jamscripth;-)关于另一个问题/答案,我们很快再见;-)