Today I have a security issue such as C ++, and I am wondering if there is a good way to get the compiler to detect this problem at compile time. Consider this code example:
class Bar { public: void Foo(bool arg1 = false, int arg2 = 10, int arg3 = 20) { [...] } }; int main(int argc, char ** argv) { int x = 40, y = 50; Bar b; b.Foo();
As shown in the last call to b.Foo (), the problem is that it is easy to forget to provide the first argument, in which case everything goes wrong as it is not caught by the compiler.
Which would be nice if I could get the compiler to say something like "ERROR, a non-Boolean value was passed to a boolean parameter". This would force the developer to learn the code, and if he really wanted to pass x as a logical one, he would have to go (x! = 0) instead.
This seems like a good place to use the "explicit" keyword, but AFAICT this keyword does not work for function arguments.
(I understand that this kind of problem could be avoided by not supplying default values for the arguments, but the default values can be quite useful)
c ++
Jeremy friesner
source share