472,353 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Any way to print out template typename value?

463 Expert 256MB
I have a simple template method that looks like
Expand|Select|Wrap|Line Numbers
  1.     template < typename T>
  2.     void WhatEver( void)
  3.     {
  4.          T t;
  5.          // How to print with cout the T value (not the t value)?
  6.     }
I thought maybe RTTI would work, but that deals with classes with virtual methods. Since T is most likely going to be an int or double, is there any built in C++ way to print out the value of T.
Dec 8 '09 #1
5 16262
9,065 Expert Mod 8TB
I assume you mean to actually output the typename used by the class in which case the answer is basically no.
Dec 8 '09 #2
256 100+
It might not be exactly what you want, but gcc provides __PRETTY_FUNCTION__ which ends up printing the type of T for you:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  5. template <typename T>
  6. void WhatEver(void)
  7. {
  8.     T t;
  9.     cout << __PRETTY_FUNCTION__ << endl;
  10. }
  12. int main()
  13. {
  14.     WhatEver<int>();
  15.     WhatEver<long>();
  16.     WhatEver<string>();
  18.     return 0;
  19. }

void WhatEver() [with T = int]
void WhatEver() [with T = long int]
void WhatEver() [with T = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]

If you aren't using gcc maybe you can look under it's hood to find out how __PRETTY_FUNCTION__ works?
Dec 8 '09 #3
9,208 Expert Mod 8TB
This is C++. Use the typeid keyword to see the type of the variable at run time:

Expand|Select|Wrap|Line Numbers
  1. template < typename T> 
  2.     void WhatEver( void) 
  3.     { 
  4.          T t; 
  5.          // How to print with cout the T value (not the t value)? 
  6.          cout << typeid(t).name() << endl;    //Your answer
  7.     } 
  8. int main()
  9. {
  10.     WhatEver<int>();
  11.     WhatEver<double>();
  12. }
Research C++ RTTI for further info.
Dec 8 '09 #4
463 Expert 256MB
This works fine, but the output is not very recognizable. With this format, typeid gives you a name of i for int, j for unsigned int, c for char, and (oh yes) h for unsigned char.

GNU g++ uses the Itaniium C++ ABI (http://www.codesourcery.com/public/cxx-abi/abi.html). Go past the mangled name section for more info about the encodings.

It appears to be a format designed to print out any variable defined in the know universe. Whew!
Dec 8 '09 #5
9,208 Expert Mod 8TB
The name() method is implementation dependent. The idea is that youonly use it for debugging. Otherwise, you are supposed to compare the type_info object returned by typeid with one created from a known type. The removes the dependence on the implementation.

Expand|Select|Wrap|Line Numbers
  1. if ( typeid(int) == typeid(myVar) )
  2. {
  3.      //etc...
  4. }
BTW: Visual Studio typeid name() method returns int for int unsigned char for unsigned char, etc.
Dec 8 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Alex Vinokur | last post by:
========================================= Windows 2000 CYGWIN_NT-5.0 1.3.22(0.78/3/2) GNU gcc version 3.2 20020927 (prerelease)...
by: Gianni Mariani | last post by:
I remember seeing a neat template specialization trick posted here a few months ago that allowed the detection of a type containing a member. After...
by: Alexander Stippler | last post by:
Hi, what do I have to do to get this (incorrect) piece of code to work. The specialization is wrong, but how can I do it? template <typename...
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of...
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual...
by: tutufan | last post by:
Hi, I'm trying to use template metaprogramming to print (for debugging purposes) the contents of an arbitrary (random-access) container. So,...
by: Howard Gardner | last post by:
// I think that there is no way to write the template "cant_write_me" in // this example. Would love to be wrong. // one of many approaches that...
by: Peter Olcott | last post by:
http://groups.google.com/group/comp.lang.c++/msg/a9092f0f6c9bf13a I think that the operator() member function does not work correctly, does...
by: =?UTF-8?B?SmVucyBNw7xsbGVy?= | last post by:
(I also posted this to boost-user) The BGL implementation of breadth-first search uses a dedicated color map. I had the following idea: Some...
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.