std namespace is notorious C++?

Or in any case to a very large portion of the programming population. And as much as we are “progressing” with dramatically changing C++98 from 11 to 17 and 20, 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, as an large set of abstractions written in C++.

But here is a problem. Only portion of  “normal” developers use C++. And even smaller portion do understand it.

For a newcomer, c++ std lib 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,  from another string. And I know how to do this and I know how to use <algorithms> part of the std namespace.

But let us assume it was you faced with this task. You the C++ beginner.  To be the C++ “beginner” is a level high enough to be a master in some other programming languages. So you asked dear “Google”.

The code  you pasted uses standard C strings.  So “by the book” usage of std::find_first_of() will “of course” look like this:

Working and absolutely horrible code.  Yes it does show how to  properly use std::find_first_of() . It also makes every developer newcomer to C++ wishing NOT to learn C++.

If he or she tries the above, code, normal looking to the untrained eye, there will be warnings. Identical or very similar to this:

There might be troubles ahead…

We could rectify this situation and considerably prolong this post in trying to do it in an pedagogical manner. But we wont.

But, compiler permitting the warnings, this code indeed works and supposedly shows the “genericity” of the std solutions because it does not require as mandatory the use of std::string, etc. But in the same time, it is an epitome of the C++ anti marketing problem.

If I may help?

Above is one questionable 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 C strings. And nothing else but C strings.

Slight detour

If that was the requirement here is the solution.

Yes this works. It is fast and simple. Done. But it is not generic.  Maybe you do not, but if you need a generic solution please read on.

Generic Solution

Then, I have decided it would be much better to provide a humble and 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. Let’s try first with string literals.

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 an rudimentary utility in checking if something is a proper sequence or not. Some user-defined abstraction perhaps. This line of code :

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 (hint: see the diagram above). Here is how you will use it with the two arrays of int’s.

I have deliberately not used here an SFINAE solution, to stop callers using this function with no legal sequences. I suggest instead we wait for C++20 constraints.

That will be a much more fun way vs trying to find and read the relevant sentence from pages of C++ compiler errors. And for me, C++20 delivers shorter and simpler code.

The whole C++17 program, is here.

This implementation is quite simple and good opportunity to learn C++ and other std namespace goodies. And also and importantly 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 and forcing the understanding of the std lib first.

Similar kind of higher-level, helper wrappers can be applied to almost each and every function from the <algorithms> part of the std lib.

Think abstract!