📜 ⬆️ ⬇️

A simple replacement for Boost :: Optional for using nullable types in C ++ projects

The article tells the story of how the author frightened the monstrous Boost and how he came up with his little nullable-bike. Perhaps the article will be useful to those who want to use simple nullable-types in their project without using Boost, meeting the requirements given in the article are not the simplest.

So, starting to support one relatively large MFC project that implements a thin billing client, I found that every time you need to pass a NULL to the database, there is an additional code containing this type check (conditionally):

//... void CSomeClass::someFunction(double variable) { //... if (variable == 0) { db->addValue(NULL); } else { db->addValue(variable); } //… 

When I was faced with the need to implement a new feature in this project that requires reading and writing NULL values, I immediately wanted to build in the project support for nullable types, which would provide a simple and safe solution in terms of memory leaks to create them, assign them different values, copying and transferring to those ready-made functions that take a pointer to the value in memory as input.

Until now, I have not had to use the Boost library, but I heard about it and thought: “Great, it's time to touch it”. Moreover, the “google” on the topic of “c ++ nullable type” somehow boldly hints at Boost :: Optional.
')
However, I quickly realized that Boost :: Optional does not suit me (and / or does not like it). Firstly, its study and connection to the project was not included in the assessment of the work time for the task, secondly, I realized (perhaps I was mistaken here) that I would get problems when converting from Boost :: Optional to the usual “pointing” type C ( at a minimum, it will not look very beautiful in code), in other words, I was not at all prepared for the fact that I would encounter difficulty for me in using it. Maybe the whole thing is that I had Jeff Alger’s book “C ++ for real programmers” read behind, there were various solutions in my head for implementing all sorts of smart (and not so) pointers and operator overloading, and therefore I didn’t understand why for the implementation of the nullable-type, which I saw quite simple (for my needs), you need a monster like Boost :: Optional.

And my needs are as follows (immediately using the example of using my nullable-bike):

 class FooModel { public: NullableType<int> intParam; }; FooModel* model = new FooModel(); // ,  -  nullable-   NULL bool resBool = model->intParam == nullptr; //        NULL model->intParam = 654; //  “   ”   .  resBool = model->intParam == nullptr; //    “   ”  NULL,      NullableType<int> globIntParam1 = model->intParam; //    ,        int* intVarPtr1 = new int(23); //      C- int* intVarPtr2 = NULL; model->intParam = intVarPtr1; //       model->intParam = intVarPtr2; //   . (    ,  ) NullableType<int> globIntParam2(intVarPtr); //        

I need a simple wrapper over the built-in simple C types that allow them to be assigned a NULL value, something like an int? or double? in C #, without the support of assigning NULL to "anything." (Just in case, I will say that there can be no talk of using features of C ++ 11 or higher). A little googling, I found a couple of similar implementations, but I was confused by the lack of all the features I needed and I wrote my own implementation of nullable-wrappers.

If I am right, then something like this is needed in many C ++ projects. Therefore, it is hard to imagine that so far Google has not given a simple solution to this issue.

My wrapper has a problem - it cannot be used without a mind with strings, since when initializing a string "wrapped" into it with a NULL value, you need to do something so that when you try to convert to string, you get an empty string from a NULL. In the comments to the source there is a quick solution (checked in gcc and clang, everything is OK), but it may not please everyone. This problem can also be solved for MFC privately by adding support for CString, which it uses instead of the usual string, but I didn’t do it not to bind it to MFC, and even now I didn’t need to write a NULL value. in the database instead of the empty string.

Once again I will be to various proposals and criticism, perhaps I did not put enough effort to find a ready-made simple solution to this problem.

The source code for the described nullable class is here .

Source: https://habr.com/ru/post/262443/


All Articles