2

I am developing a project using C++98 . So I have no "nullptr". Of course I do a static code analysis for the source code in my project. From that I got many messages regarding the usage of 0 in assignments and comparisons. All messages are related to "different types". I started to add suffixes to 0, to define some correct types. But this is not possible for all build in data types. And especially not for pointers (Yes, raw pointer).

I then came up with the following template:

template <typename T>
const T null(void) 
{
    return static_cast<T>(0);
}

With that I get no more warnings regarding mixing up types. I can write for example:

typedef unsigned char uchar;
. . . 
uchar someVariable = null<uchar>();
SomeComplexClass *someComplexClass = null<SomeComplexClass *>();
. . . 
if (null<SomeComplexClass *>() == someComplexClass)
{
    . . . 
}

You may ask, omg, what is this nonesense. But being a Functional Safety Expert and working in the automotive industry, I am getting more and more cautios, even in small hobby home projects, like the above.

After reading Scott Meyers "Effective Modern C++", especially "Item 1: Understand template type deduction.", I am not any longer sure, if this template will work in any case. The compiler (gcc, raspbian) does of course always remove this stuff and replaces it with plain 0 in the object code. But

  • Will the syntax be always OK?
  • For any type?
  • What about the constness?
  • Any other solution?

I even have concerns regarding "nullptr". I think I am getting a little paranoid :-)

A M
  • 14,694
  • 5
  • 19
  • 44
  • What do you mean by the syntax being "OK"? What syntax? – melpomene Nov 03 '18 at 09:11
  • 1
    For one, there is no template type deduction here. You always provide the type explicitly. Beyond that I really don't get what this post is about. – StoryTeller - Unslander Monica Nov 03 '18 at 09:18
  • If I use any type for T and then use the template for example in a comparison or assignment. With any kind of qualifier. Will that always work? – A M Nov 03 '18 at 09:19
  • The post is about comparing 0 apples to 0 pies. I did not mean to talk about type deduction for this example. Maybe it was misleading to mention the chapter in the book. Sorry for that, – A M Nov 03 '18 at 09:24
  • Possible duplicate of [implementation safe nullptr](https://stackoverflow.com/questions/13675203/implementation-safe-nullptr) – OznOg Nov 03 '18 at 09:53
  • probably already discussed https://codereview.stackexchange.com/questions/19316/custom-nullptr-t-class https://stackoverflow.com/questions/13675203/implementation-safe-nullptr – OznOg Nov 03 '18 at 09:54
  • No. I do not want a nullptr that is compatible to any type. I want exactly the opposite. I want a 0 or a pointer to 0 compatble to just one specific type. Seems that I need to learn a better English. Sorry for that – A M Nov 03 '18 at 10:41
  • In the past (pre-C++11) at times I've use this (dubious?) technique: `Foo* const NULL_Foo = 0;` and then `if (pFoo == NULL_Foo) ...`. It was more for self-documenting code than to try to get the compiler to perform more type safety at compile time. – Eljay Nov 03 '18 at 13:01

0 Answers0