How Google deals with errors in C++

Google does not use exception in their C++ code base. For errors, they use a class called status than the programmer must check when it is returned from a function. Otherwise the program does not compile (link https://www.youtube.com/watch?v=NOCElcMcFik at 41:34). I have a few questions:

1) Is there any example of that class on the web freely available?

2) That's okay for "void f()" that work with side effects that you turn into a "Status f()". But what if your function already returns a value? Google does not allow to pass references that are not const so you can't mutate a Status object given to you. So how do they do?

Thanks for your help.


From Google style guide:

Input parameters are usually values or const references, while output and input/output parameters will be non-const pointers.

As the lecturer said Google uses a proprietary in-house compiler that has been rigged to throw errors when Status isn't checked.


1) Like what has been mentioned before, you would need custom tooling to enforce such rules. These could be code analysis rules that can be set to raise errors when failing.

2) There are ways to return multiple values in C++. You can either return a pair where one item is the value you care about, and the other is a status object. C++11 also introduced tuples for even more return values. You could even write your own object containing all the returned information you might need, but this could end up being overkill in many scenarios.

Most likely, Google would have you pass in a pointer instead of a non-const reference. I believe that they prefer this style because it forces the caller to pass in the address of an object so it is more explicit that a "reference" to the object is being used and that the object might be modified.

// let's a code reviewer know
// that a's address is used and might be modified
f(&a)

// requires the code reviewer to know
// the function signature to determine
// if the a is passed by value,
// const reference, or non-const reference
f(a)
链接地址: http://www.djcxy.com/p/84766.html

上一篇: 带有Google Apps脚本的JQuery Mobile

下一篇: Google如何处理C ++中的错误