std namespace is notorious C++?

Or in any case to a very large portion of programming population. And as much as we are “progressing” with dramatically changing C++98 from 11 to 17 and beyond, that much it seems people at large are regressing in grasping the concepts of C++ standard library. 

Two sequences. Also showing what is the first and last iterator position.

One of the key reasons is simple: abstract thinking is hard.

And C++ is based on the idea that fundamental abstractions and concepts if presented in a language are the best building blocks for software architecting and building art.

So, the C++ standard library is carefully selected and presented, an alarge set of key abstractions written in C++.

For a newcomer, this is one very large mountain to cross.

The other day, I had yet another one of the numerous needs (in my code) to find something in a string by looking into another string. And I know how to do this and I know how to use <algorithms> part of the std namespace.

The code in question uses standard C char arrays.  So “by the book” usage of std::find_first_of() will “of course” look like this:

Working and absolutely horrible. It does use std::find_first_of() but makes every developer newcomer to C++ wishing NOT to learn C++. This code indeed works and shows the genericity of the std solutions because it does not require as mandatory the use of std::string, etc.

This indeed is not generic at all but one efficient peace of code. Partially because it has no sanity checking at all.  It is just a  display of the required way on how to use the std::find_first_of() with char arrays. And nothing else but char arrays.

Then, I have decided it would be much better to provide simple helper function that will offer an immediate remedy and at the same time show, why is abstract thinking important and how that importance proves itself in practice.

One simple in-line helper providing an immediate remedy to the painful and repeatedly painful, usage of one std function. The usage of std::find_first_of() suddenly becomes “normal” code.

And that little helper is actually quite generic. It will work on anything that conforms to the C++ definition of a “sequence”. Thus also providing a utility in checking if something is a proper sequence or not. Some user-defined abstraction perhaps. This line of code in use:

Will indeed make the compiler to try and use “anything” you throw at it. Far beyond char arrays. But both sequences have to conform to C++ rules. And if and when this does not compile, it will make you learn. Perhaps in a much more fun way vs trying to find and read the relevant paragraph from C++ iso standard. And for me, this delivers shorter and simpler code.

The whole program, showing this in action, is here.

This implementation is quite simple and good opportunity to learn C++ and other std namespace goodies. And also showing how a little bit of abstract thinking and knowledge of std namespace “composability”,  makes std namespace so useful.

Just if you do know how to look at it.

I think this is a much better approach to explaining C++, vs not showing a working solution but forcing the understanding of std namespace first.

ps: yes you have noticed. Similar kind of helper wrappers can be applied to almost each and every function from the <algorithms> part of the std lib.



Leave a Reply