如何检查函数参数是否恒定?

Wel*_*n40 2 c++ const function

这可能很容易,我可能只是错过了我面前的事情.但是,我无法弄清楚如何确保通过引用传递的函数参数可以被修改.基本上我需要以下内容:

bool calculate(double lat, double lon, double dep, 
               double &x, double &y, double &z)
{
    if (x, y, AND z are NOT const)
    {
        perform the proper calculations
        assign x, y, and z their new values
        return true;
    }
    else //x, y, or z are const
    {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

"if"语句检查确实是我所需要的

我再次道歉,如果这已经在这个网站上,或者它是一个标准的库函数,我在我面前失踪.我一直来到这里,几乎总能找到一个好的答案,但我在这里找不到任何相关的东西.

rab*_*sky 8

如果你有,double &x那么它不是一个常数.如果你有,const double &x那么它是一个常数.

在你的情况下 - 不需要检查,它们不是常量.检查在编译时自动执行.

看到这个:

void func(double &x){  X=3.14;  }

double d;
const double c_d;
func(d); // this is OK
func(1.54); // this will give an ERROR in compilation
func(c_d);  // this will also give an ERROR in compilation
Run Code Online (Sandbox Code Playgroud)

你根本无法调用想要const一个常量(非)引用的函数.

这意味着编译器会为您找到一些错误 - 比如在您的情况下,您不必返回true或者false,并且您不需要检查它以尝试查找错误 - 您只需编译并且编译器将找到这些错误为了你.