Article View: comp.sys.mac.programmer.codewarrior
Article #1486typeid & template func woes
From: Conrad Weyns
Date: Wed, 12 Feb 2003 12:52
Date: Wed, 12 Feb 2003 12:52
154 lines
4762 bytes
4762 bytes
compiler: CW Pro 8.3 Mac & Win.
issue: how can I get hold of the real type name of T that is part of a
function signature in the following stripped to the bone but working code:
Here follows the example code, I'll discuss the issue at the end.
------------- main.cpp ------------
#include <iostream>
using namespace std;
template <typename T> void _report(T& _t)
{
static long sWoes = 999;
cout << " function name: " << __PRETTY_FUNCTION__ << endl
<< " using __typeof__: " << typeid(__typeof__(T())).name() << endl
<< " sWoes address: " << &sWoes << " sWoes = " << sWoes << endl
<< " type of _t: " << typeid(_t).name() << endl
<< " value of _t "<< _t << endl;
}
class TestA
{
public:
long x;
TestA(long _x) : x(_x) {}
void report();
void const_report() const;
};
void TestA::report()
{
_report(x);
}
void TestA::const_report() const
{
_report(x);
}
class TestB
{
public:
long x;
TestB(long _x) : x(_x) {}
void report();
void const_report() const;
};
void TestB::report()
{
_report(x);
}
void TestB::const_report() const
{
_report(x);
}
void doTest();
void doTest()
{
TestA tA(123);
cout << "1A. tA.report() gives: " << endl;
tA.report();
cout << endl << "2A. tA.const_report() gives: " << endl;
tA.const_report();
cout << endl;
TestB tB(123);
cout << "1B. tB.report() gives: " << endl;
tB.report();
cout << endl << "2B. tB.const_report() gives: " << endl;
tB.const_report();
cout << endl << "beats me! :-)" << endl;
}
int main()
{
doTest();
return 0;
}
-----------------------------
Output of this running program:
1A. tA.report() gives:
function name: _report<long>(long &)
using __typeof__: long
sWoes address: 0x00420460 sWoes = 999
type of _t: long
value of _t 123
2A. tA.const_report() gives:
function name: _report<const long>(const long &)
using __typeof__: long
sWoes address: 0x00420454 sWoes = 999
type of _t: long
value of _t 123
1B. tB.report() gives:
function name: _report<long>(long &)
using __typeof__: long
sWoes address: 0x00420460 sWoes = 999
type of _t: long
value of _t 123
2B. tB.const_report() gives:
function name: _report<const long>(const long &)
using __typeof__: long
sWoes address: 0x00420454 sWoes = 999
type of _t: long
value of _t 123
beats me! :-)
------------------------
What is crusial here is the fact that report() and const_report()
instantiate 2 different template functions.
In report(), the function signature is of type <long>(long&).
In const_report(), it is of type <const long>(const long&)
So far, so good.
The problem is that in both cases the type of the input parameter _t
reported by typeid is identical: long.
This is probably correct, I couldn't possibly tell from a lanuage point of
view. It is however a bummer, because instead of getting 1 single function
scope static variable sWoes across an entire application, I get 2. Now this
would be ok, indeed required, if they represented a different type _t. But
they don't. The result is that I end up with 2 entities representing the
same type _t, a long in the above example.
Now assuming that this is *not* a compiler bug, but a programmer falling
flat on his face yet again ... how can I get the type of T as evident in
the function name reported by the __PRETTY_FUNCTION__ macro.
In other words, inside the _report() template function in the given example
I'd like to get hold of the "<long>" and <const long> part since
typeid(_t).name() is misleading me.
Cheers,
Conrad Weyns
Technical Details
Message-ID:
<b2dg2g$1117$1@news.dataguard.no>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed.stueberl.de!fu-berlin.de!news.dataguard.no!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f635e,d1f7d1bdb1e475a0,start X-Google-Attributes: gidf635e,public X-Google-Thread: fd047,d1f7d1bdb1e475a0,start X-Google-Attributes: gidfd047,public X-Google-ArrivalTime: 2003-02-12 04:52:35 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!news.maxwell.syr.edu!newsfeed.stueberl.de!fu-berlin.de!news.dataguard.no!not-for-mail From: "Conrad Weyns" <weyns@online.no> Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.windows Subject: typeid & template func woes Date: Wed, 12 Feb 2003 13:52:29 +0100 Lines: 161 Message-ID: <b2dg2g$1117$1@news.dataguard.no> NNTP-Posting-Host: 217.8.149.124 X-Trace: news.dataguard.no 1045054352 33831 217.8.149.124 (12 Feb 2003 12:52:32 GMT) X-Complaints-To: news-abuse@dataguard.no NNTP-Posting-Date: Wed, 12 Feb 2003 12:52:32 +0000 (UTC) X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2720.3000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000