简体   繁体   English

API更改上的函数参数的常量正确性

[英]Const-correctness of function parameters on API changes

Suppose I am using a library which implements the function foo , and my code could look something like this: 假设我正在使用实现foo函数的库,并且我的代码可能如下所示:

void foo(const int &) { }

int main() {
    int x = 1;
    foo(x);
    std::cout << (1/x) << std::endl;
}

Everything works fine. 一切正常。 But now suppose at one point either foo gets modified or overloaded for some reason. 但是现在假设foo由于某种原因而被修改或过载。 Now what we get could be something like this: 现在我们得到的可能是这样的:

void foo(int & x) {
    x--;
}
void foo(const int &) {}

int main() {
    int x = 1;
    foo(x);
    std::cout << (1/x) << std::endl;
}

BAM. BAM。 Suddenly the program breaks. 程序突然中断。 This is because what we actually wanted to pass in that snippet was a constant reference, but with the API change suddenly the compiler selects the version we don't want and the program breaks unexpectedly. 这是因为我们实际上想要传递的代码段是一个常量引用,但是随着API的更改,编译器突然选择了我们不需要的版本,并且程序意外中断。

What we wanted was actually this: 我们想要的实际上是这样的:

int main() {
    int x = 1;
    foo(static_cast<const int &>(x));
    std::cout << (1/x) << std::endl;
}

With this fix, the program starts working again. 有了此修复程序,程序将再次开始工作。 However, I must say I've not seen many of these casts around in code, as everybody seems to simply trust this type of errors not to happen. 但是,我必须说,我在代码中没有看到很多这样的强制转换,因为每个人似乎都简单地相信不会发生这种类型的错误。 In addition, this seems needlessly verbose, and if there's more than one parameter and names start to become longer, function calls get really messy. 另外,这似乎是不必要的冗长,并且如果有多个参数并且名称开始变长,函数调用就会变得非常混乱。

Is this a reasonable concern and how should I go about it? 这是一个合理的问题,我应该如何解决?

If you change a function that takes a const reference so that it no longer is a const, you are likely to break things. 如果更改采用const引用的函数,使其不再是const,则很可能会破坏事情。 This means you have to inspect EVERY place where that function is called, and ensure that it is safe. 这意味着您必须检查调用该函数的每个位置,并确保它是安全的。 Further having two functions with the same name, one with const and one without const in this sort of scenario is definitely a bad plan. 在这种情况下,进一步具有两个具有相同名称的函数,一个具有const,一个不具有const,绝对是一个糟糕的计划。

The correct thing to do is to create a new function, which does the x-- variant, with a different name from the existing one. 正确的做法是创建一个新函数,该函数执行x--变体,其名称与现有函数的名称不同。

Any API supplier that does something like this should be severely and physically punished, possibly with slightly less violence involved if there is a BIG notice in the documentation saying "We have changed function foo , it now decrements x unless the parameter is cast to const". 如果这样做的任何API供应商都应受到严厉的人身惩罚,如果文档中有BIG通知说“我们已更改函数foo ,则它现在将x递减,除非将参数强制转换为const”,因此可能会受到严厉的肢体惩罚。 。 It's one of the worst possible binary breaks one can imagine (in terms of "it'll be terribly hard to find out what went wrong"). 这是人们可以想象的最糟糕的二进制中断之一(就“很难找出问题所在”而言)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM