BDE 4.14.0 Production release
|
Provide a NameOf
type for displaying template type at run-time.
@FREE FUNCTIONS: bsls::nameOfType(const TYPE&): template function to return name of TYPE
This component provides a template class, bsls::NameOf<TYPE>
, which can implicitly cast to a const char *
which will point to a description of TYPE
.
This section illustrates intended usage of this component.
First, your test driver must have the following using
statements so that the template class NameOf
and the template function nameOfType
can be referred to concisely, without having to qualify them with namespaces on each call. Note that if you've already said using namespace BloombergLP
you don't have to give the BloombergLP::
qualifiers here:
Next, we define some types in the unnamed namespace:
Next, we see that the NameOf
template class, when created with a type, can be implicitly cast to a const char *
which points to a description of the type.
Then, we see that when NameOf
is passed a typedef
or template parameter, it resolves it to the original type:
Next, we introduce the nameOfType
template function, which takes as any variable as an argument, and returns a const char *
pointing to a description of the type of the variable.
Then, we see that NameOf
and nameOfType
will strip BloombergLP::
namespace qualifiers, as well as anonymous namespace qualifiers.
There is a problem with template code not knowing how to implicitly cast the NameOf
type to const char *
for initializing or comparing with std::string
s. To facilitate, NameOf
provides a const char *
name
accessor, to avoid the user having to do a more verbose static cast
.
Note that nameOfType
naturally returns a const char *
and needs no help casting. Note also that bsls::debugprint
is able to figure out how to cast NameOf
directly to const char *
with no problems, as can iostreams, so there is no problem with putting a NameOf
in a LOOP_ASSERT
or ASSERTV
. It is anticipated that displaying by the BDE ASSERTV
, LOOP_ASSERT, and
P' macros will be the primary use of this component.
Which produces:
Finally, we see that NameOf
and nameOfType
will simplify std::basic_string<...>
declarations to std::string
.