Why Boost uses a global override function to implement custom validators in Program Options

This example shows a named function validate

defined in the global scope to overload a function in a namespace boost::program_options

.

What justifies this project using a global overload function? Why a stricter design not implemented instead boost::program_options

(like overriding a class method or some other schema)?

As noted in the comments below, my main problem is that the user might be surprised to find that one of their global functions is being called by the library.

It should be emphasized that free named functions are extremely important (as opposed to free global scope functions, see this link provided by Chris Drew). Indeed, named, non-class (free) functions are IMO the main advantage of C ++.

My company is considering adopting Boost, it seems to be highly regarded. But this particular design decision interested me.

+3


source to share


2 answers


I think my main surprise is that a function defined in a namespace can be overloaded (hidden) by a function defined in a global scope. I'm going to ask why C ++ allows this, and if anyone thinks it's a disadvantage if you don't have a concise explanation. user2141130 2 hours ago

No, this cannot be considered a disadvantage.

The function will not be hidden by the function defined in the global scope.

This is what Dependent Argument Search (ADL) is all about, and you've used it all over the place! You are using it here:



std::cout << "Hello world!";

      

ADL is pretty thin and pretty common. Many people don't realize this until they ask themselves the same question you just asked. The loose function works very well for extension points.

Additional Notes: What is an Argument-Dependent Lookup (such as ADL or Koenig Lookup)?

+2


source


In general, different acceleration libraries have different levels of quality.

In particular, this applies to the relatively rare case of the user. I think it could have been implemented differently, but the author chose a quick and easy route. While I would agree that a global function is not perfect, it certainly isn't as bad as, say, a global variable. These features validate

will be limited main.cpp

or similar in nature .



Of course, you can either suggest improvements to the library or use alternative implementations. I can assure you that there are a lot of useful and well thought out libraries in boost, so don't be discouraged by this example.

+1


source







All Articles