type_name? no!

S

Stefan Ram

They have several type traits in C++. But what I often have
been missing for decades is a means to print the type of an
expression. Why do they not have a trait for a name of at
least the most common types?

#include <iostream>
#include <ostream>

template< typename T >struct type_name
{ static const bool is_specialization = false; };

template<> struct type_name< int >
{ static const bool is_specialization = true;
static constexpr char const * text = "int"; };

template<> struct type_name< long >
{ static const bool is_specialization = true;
static constexpr char const * text = "long"; };

int main()
{ { int i; ::std::cout << type_name< decltype( i )>::text << '\n'; }
{ long i; ::std::cout << type_name< decltype( i )>::text << '\n'; }}

prints

int
long
 
I

Ian Collins

Stefan said:
They have several type traits in C++. But what I often have
been missing for decades is a means to print the type of an
expression. Why do they not have a trait for a name of at
least the most common types?

I'm not sure what you are asking, but wouldn't it be better to have
standardised the output of typeid::name()? So something like

std::cout << "int: " << typeid(1).name() << std::endl;

would give a consistent result.
 
V

Victor Bazarov

I'm not sure what you are asking, but wouldn't it be better to have
standardised the output of typeid::name()? So something like

std::cout << "int: " << typeid(1).name() << std::endl;

would give a consistent result.

Consistent with what? There were no requirements that 'name' member of
'type_info' actually has any *particular* implementation, last time I
looked. It does return "a null-terminated multibyte string", but that's
about it.

V
 
I

Ian Collins

Victor said:
Consistent with what? There were no requirements that 'name' member of
'type_info' actually has any *particular* implementation, last time I
looked. It does return "a null-terminated multibyte string", but that's
about it.

That was my point. If there was a standardised requirement for built in
types, there wouldn't be a need for additional traits.
 
S

Stefan Ram

Ian Collins said:
That was my point. If there was a standardised requirement for built in
types, there wouldn't be a need for additional traits.

Anyways, I was not aware of typeid( expr ).name, and it might
help me sometimes. Thinking about modern type traits made me
forget about »good old« RTTI.
 
Ö

Öö Tiib

That was my point. If there was a standardised requirement for built in
types, there wouldn't be a need for additional traits.

+1
Why to avoid writing down some requirements to 'typeid::name()'? It can
not be considered so huge task. Some canonical format for type names.
 
V

Victor Bazarov

With all such expressions within the program? (Iow. if the parameter is
of type int, it will always give the same result.)

Well, how would you know it's the same result and not as, say, of your
user-defined type, if both return the same empty byte sequence? And
there is nothing in the Standard that says that they shall be different.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top