C++ 类型定义不清楚

标签 c++ types

在下面的 C++ 代码中,double (*) double 是什么意思? 它是什么样的返回类型?

auto get_fun(int arg) -> double (*)(double) // same as: double (*get_fun(int))(double)
{
    switch (arg)
    {
        case 1: return std::fabs;
        case 2: return std::sin;
        default: return std::cos;
    }
}

最佳答案

double (*)(double) 它是一个函数指针签名,用于接受一个 double 参数并返回 double 的函数。一般

X (*)(A, B, C)  // any number of args

是一个指向函数的指针,该函数接受类型为 (A, B, C) 的参数并返回类型为 X 的值,例如

X my_func(A, B, C) {
    return X();  // assuming this makes sense
}

将是上面的签名。

所以在您的例子中,get_fun 是一个返回函数指针的函数。

关于C++ 类型定义不清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40303315/

相关文章:

go - GAE GO 数据存储中最大的数据类型

python - Pandas 中的 dtype ('O' ) 是什么?

ios - "' ViewController.Type ' does not have a member named . . . "

c++ - 从返回类型为 'void' 的函数获取结果,而结果变量是输入参数之一 - C++

c++ - OpenGL 着色器的 .HLSL 文件

c++ - 警告 C4673 : throwing 'ex::traced_error<EX>' the following types will not be considered at the catch site

haskell - 使用类型系统检查输出与输入列表的长度

java - 了解 Java 数据类型

c++ - 局部变量的内存地址根据 lambda 参数的存在而变化

c++ - 观察 QStringList 的新项目