473,390 Members | 878 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

Convert template argument to string

Hi there

I'm looking for a template class that converts the template argument to a
string, so something like the following should work:

Convert<float>::Get() == "float";
Convert<3>::Get() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";

The reason I need this is that in our design every class of a certain
hierarchy has a class name that must be unique. So far I could not find a
solution for template classes.

Thanks for your ideas
Marco

Oct 16 '08 #1
9 17464
Marco Nef wrote:
Hi there

I'm looking for a template class that converts the template argument to
a string, so something like the following should work:

Convert<float>::Get() == "float";
Convert<3>::Get() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";

The reason I need this is that in our design every class of a certain
hierarchy has a class name that must be unique. So far I could not find
a solution for template classes.
You have to provide one or use typeid.

--
Ian Collins
Oct 16 '08 #2
Marco Nef schrieb:
I'm looking for a template class that converts the template argument to
a string, so something like the following should work:

Convert<float>::Get() == "float";
Convert<3>::Get() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";

The reason I need this is that in our design every class of a certain
hierarchy has a class name that must be unique. So far I could not find
a solution for template classes.
Solution 1:

Use RTTI. With typeid(...).name() you will get a unique name. However,
this name is compiler dependant and may not look pretty at all. If you
don't care about that, it is fine. (Of course, the lookup is done at
runtime.)
Solution 2:

The good old macros.
#define TOSTRING(x) #x
will convert anything you pass to it into a string, e.g. TOSTRING(float)
will evaluate to "float" at compile time. But you will not manage to
take care of namespaces this way. The macro will always return the
string as it is - with or w/o namespace prefix.
Marcel
Oct 16 '08 #3
On Oct 16, 7:43*am, "Marco Nef" <maill...@shima.chwrote:
I'm looking for a template class that converts the template argument to a
string, so something like the following should work:

Convert<float>::Get() == "float";
Convert<3>::Get() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";

The reason I need this is that in our design every class of a certain
hierarchy has a class name that must be unique. So far I could not find a
solution for template classes.
What do you need those names for? Why do you want for it to work with
values as well as with types?

If typeid(X).name() is not sufficient for you (not portable, may be
not human readable), you'll have to provide a string for every class
yourself.

--
Max
Oct 16 '08 #4
I'm looking for a template class that converts the template argument to
a
string, so something like the following should work:

Convert<float>::Get() == "float";
Convert<3>::Get() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";

The reason I need this is that in our design every class of a certain
hierarchy has a class name that must be unique. So far I could not find
a
solution for template classes.

What do you need those names for? Why do you want for it to work with
values as well as with types?

If typeid(X).name() is not sufficient for you (not portable, may be
not human readable), you'll have to provide a string for every class
yourself.

--
Max
Thanks for your replies. I know about typeid and it is not sufficient as
"typeid->name" by the standard is not persistent and not even unique. The
macro "TOSTRING(x) #x" does not work because it is evaluated before the
template process works. This means that "Convert<float>::Get()" returns
"Type" if Type is the name of the template argument.

Another solution would be the following:

template<typename Typestruct Convert
{
const String &GetName(VOID) const { return Type::GetName(); }
};
template<struct Convert<float>
{
const String &GetName(VOID) const { return "float"; }
};
template<struct Convert<int>
{
const String &GetName(VOID) const { return "int"; }
};

and so on...

So any classes provided as template argument need to implement GetName(),
for the base types there are specializations. This works, but I don't really
like it.

I would love a struct like:

template<typename Typestruct Convert
{
const String &GetName(VOID) const { return XXXXXX; }
};

where XXXXXX is the question of this task :-)

Marco

Oct 16 '08 #5
On Oct 16, 9:57*am, "Marco Nef" <maill...@shima.chwrote:
I'm looking for a template class that converts the template argument to
a
string, so something like the following should work:
Convert<float>::Get() == "float";
Convert<3>::Get() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";
The reason I need this is that in our design every class of a certain
hierarchy has a class name that must be unique. So far I could not find
a
solution for template classes.
What do you need those names for? Why do you want for it to work with
values as well as with types?
If typeid(X).name() is not sufficient for you (not portable, may be
not human readable), you'll have to provide a string for every class
yourself.

Thanks for your replies. I know about typeid and it is not sufficient as
"typeid->name" by the standard is not persistent and not even unique. The
macro "TOSTRING(x) #x" does not work because it is evaluated before the
template process works. This means that "Convert<float>::Get()" returns
"Type" if Type is the name of the template argument.

Another solution would be the following:

template<typename Typestruct Convert
{
* * const String &GetName(VOID) const { return Type::GetName(); }};

template<struct Convert<float>
{
* * const String &GetName(VOID) const { return "float"; }};

template<struct Convert<int>
{
* * const String &GetName(VOID) const { return "int"; }

};

and so on...
Please note, that in C++ you don't need (void) for functions with no
arguments. () is sufficient.

These GetName() functions return a reference to a non-existent
constant String, which is a programming error.
So any classes provided as template argument need to implement GetName(),
for the base types there are specializations. This works, but I don't really
like it.
You don't really need to provide a name for each specialisation,
because it can be generated in a generic fashion. Here is an example:

#include <string>
#include <iostream>

template<class T>
struct TypeName
{
static std::string get() { return T::typeName(); }
};

// add more specialisations for the build-in types
template<std::string TypeName<int>::get() { return "int"; }

struct A
{
static std::string typeName() { return "A"; }
};

template<class T>
struct B
{
static std::string typeName() { return "B<" +
TypeName<T>::get() + ">"; }
};

int main()
{
std::cout << TypeName<int>::get() << '\n';
std::cout << TypeName<A>::get() << '\n';
std::cout << TypeName<B<int::get() << '\n';
std::cout << TypeName<B<A::get() << '\n';
std::cout << TypeName<B<B<B<int ::get() << '\n';
}

Output:

int
A
B<int>
B<A>
B<B<B<int>>>

--
Max

Oct 16 '08 #6
On Oct 16, 9:28 am, Marcel Müller <news.5.ma...@spamgourmet.com>
wrote:
Marco Nef schrieb:
I'm looking for a template class that converts the template
argument to a string, so something like the following should
work:
Convert<float>::Get() == "float";
Convert<3>::Get() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";
The reason I need this is that in our design every class of
a certain hierarchy has a class name that must be unique. So
far I could not find a solution for template classes.
Solution 1:
Use RTTI. With typeid(...).name() you will get a unique name.
Maybe. I've never found a compiler where the name was
guaranteed unique, or was even unique in practice. If he's
using it in a template, however, it might be alright; templates
arguments must have external linkage, and most compilers do seem
to generate unique names for all types with external linkage.
Solution 2:

The good old macros.
#define TOSTRING(x) #x
will convert anything you pass to it into a string, e.g.
TOSTRING(float) will evaluate to "float" at compile time. But
you will not manage to take care of namespaces this way. The
macro will always return the string as it is - with or w/o
namespace prefix.
And within a template, TOSTRING(T) will result in "T",
regardless of what T actually means in the instantiation.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 16 '08 #7
On Oct 16, 10:57 am, "Marco Nef" <maill...@shima.chwrote:
I'm looking for a template class that converts the
template argument to a string, so something like the
following should work:
Convert<float>::Get() == "float";
Convert<3>::Get() == "3";
Convert<HelloWorld>::Get() == "HelloWorld";
The reason I need this is that in our design every class
of a certain hierarchy has a class name that must be
unique. So far I could not find a solution for template
classes.
What do you need those names for? Why do you want for it to
work with values as well as with types?
If typeid(X).name() is not sufficient for you (not portable,
may be not human readable), you'll have to provide a string
for every class yourself.
Thanks for your replies. I know about typeid and it is not
sufficient as "typeid->name" by the standard is not persistent
and not even unique.
What do you mean by "persistent" in this case?

And in practice, if the types are restricted to those you can
actually use to instantiate a template, about the only case
where they might not be unique in practice is if the types are
defined in anonymous namespace.
Another solution would be the following:
template<typename Typestruct Convert
{
const String &GetName(VOID) const { return Type::GetName(); }
};
template<struct Convert<float>
{
const String &GetName(VOID) const { return "float"; }
};

template<struct Convert<int>
{
const String &GetName(VOID) const { return "int"; }
};
and so on...
So any classes provided as template argument need to implement
GetName(), for the base types there are specializations. This
works, but I don't really like it.
Which is understandable. (Although you could use a macro to
generate the specializations.)
I would love a struct like:
template<typename Typestruct Convert
{
const String &GetName(VOID) const { return XXXXXX; }
};
where XXXXXX is the question of this task :-)
The only thing anywhere like this in the standard is
typeid<Type>.name().

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 16 '08 #8
Thanks for your replies. I know about typeid and it is not
sufficient as "typeid->name" by the standard is not persistent
and not even unique.

What do you mean by "persistent" in this case?
My source for that is the book "Modern C++ Design" by Andrei Alexandrescu,
p. 206: "The way type_info::name is defined makes it unsuitable for anything
other than debuggin purposes. There is no guarantee that the string is the
actual class name, and worse, there is no guarantee that the string is
unique throughout the application."
....
"There is no guarantee that typeid(Line).name() points to the same string
when the application is run twice."

Oct 16 '08 #9
On Oct 16, 2:41 pm, "Marco Nef" <maill...@shima.chwrote:
Thanks for your replies. I know about typeid and it is not
sufficient as "typeid->name" by the standard is not
persistent and not even unique.
What do you mean by "persistent" in this case?
My source for that is the book "Modern C++ Design" by Andrei
Alexandrescu, p. 206: "The way type_info::name is defined
makes it unsuitable for anything other than debuggin purposes.
There is no guarantee that the string is the actual class
name, and worse, there is no guarantee that the string is
unique throughout the application."
...
"There is no guarantee that typeid(Line).name() points to the
same string when the application is run twice."
Aha. You mean that you can't use it as a type identifier in
persistent data.

The standard says that the string is implementation defined, and
in fact, an implementation which always returned "" would be
conforming. On the other hand, there are expectations due to
quality of implementation, which no compiler would dare neglect;
at the very least, you can certainly count on the string being
the same every time the function is called for the same type,
even in different runs, at least for non-local types (as long as
you haven't recompiled with a different compiler---or a
different version of the same compiler), and on the string being
different for classes with different basic names. In practice,
as long as the class isn't local or in an anonymous namespace, I
think you're OK.

Which, admittedly isn't very much, and I generally wouldn't use
it as a type identifier in external data (transmission protocols
or persistent data).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 17 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: vpadial | last post by:
Hello, I want to build a library to help exporting c++ functions to a scripting languagge. The scripting language provides a function to register functions like: ANY f0() ANY f1(ANY) ANY...
1
by: Adam Dziendziel | last post by:
Hi all! I'm writing a luabind/boost::python-like binding utility for a Squirrel language to generating wrapper-functions for C++ classes and have a problem with passing the pointer-to-member...
3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
2
by: gao_bolin | last post by:
I have the following situation: class A { public: virtual ~A() {} }; template < typename T > class B : public A
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
4
by: George | last post by:
Dear All, I'm compiling the code below with IBM's xlC 6.0 and get the message, "rmspace.cpp", line 34.48: 1540-0298 (S) Template argument deduction cannot be performed using the function...
4
by: Damien | last post by:
Hi all, I've run into something confusing on MS VC6. Yeah I know it's old but that's what the client wants, so... I'm trying to pass a pointer to a member function as a template argument,...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
4
by: Jim Langston | last post by:
This should illistrate what I am trying to do: template <class T> T SomeFunction( T parm ) { return parm; } template <class T> class SomeClass
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.