473,326 Members | 2,173 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,326 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 17438
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.