473,396 Members | 1,706 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,396 software developers and data experts.

Any way to print out template typename value?

RRick
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.     }
  7.  
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 17413
Banfa
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
mac11
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;
  4.  
  5. template <typename T>
  6. void WhatEver(void)
  7. {
  8.     T t;
  9.     cout << __PRETTY_FUNCTION__ << endl;
  10. }
  11.  
  12. int main()
  13. {
  14.     WhatEver<int>();
  15.     WhatEver<long>();
  16.     WhatEver<string>();
  17.  
  18.     return 0;
  19. }
  20.  
output:

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
weaknessforcats
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
RRick
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
weaknessforcats
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

2
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) ========================================= Here is some program...
0
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 a day searching through google archives I come up...
17
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 T, typename V> class Mask { public: Mask(int i)...
5
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 compile-time, template computed constants. The problem is...
11
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 class 'C', but only because a trait tell us so: ...
2
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, for example, I'd like to be able to print a...
4
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 won't work template< template< typename class...
272
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 anyone else know how to make a template for making two...
1
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 algorithms don't need to distinguish black/gray,...
6
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 carlos { template <typename T>
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.