🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.sys.mac.programmer.codewarrior
5 messages
5 total messages Started by Conrad Weyns Wed, 12 Feb 2003 12:52
typeid & template func woes
#1486
Author: Conrad Weyns
Date: Wed, 12 Feb 2003 12:52
154 lines
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
Re: typeid & template func woes
#1487
Author: Miro Jurisic
Date: Wed, 12 Feb 2003 15:03
29 lines
1833 bytes
In article <b2dg2g$1117$1@news.dataguard.no>, "Conrad Weyns" <weyns@online.no> 
wrote:

> 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:
> 
> 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

My understanding is that typeof (T) == typeof (const T) == typeof (volatile T) 
== etc. Perhaps if you explain exactly what you are trying to do with this 
information we can come up with an answer. (Which will involve type traits, I am 
sure.)

meeroh
Re: typeid & template func woes
#1488
Author: Conrad Weyns
Date: Wed, 12 Feb 2003 16:34
54 lines
2684 bytes
"Miro Jurisic" <macdev@meeroh.org> skrev i melding
news:macdev-B2A383.10035512022003@senator-bedfellow.mit.edu...
> In article <b2dg2g$1117$1@news.dataguard.no>, "Conrad Weyns"
<weyns@online.no>
> wrote:
>
> > 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:
> >
> > 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
>
> My understanding is that typeof (T) == typeof (const T) == typeof
(volatile T)
> == etc. Perhaps if you explain exactly what you are trying to do with this
> information we can come up with an answer. (Which will involve type
traits, I am
> sure.)

Thanks meeroh, looks like I need to go back to school -again :-(
Ehm, I am lying now, I never went to school!

But this was the kick in the behind that I needed to get to an alternative
solution. I can simply differentiate between the two cases by supplying
*two* template functions:

template <typename T> void _report(T& _t);
template <typename T> void _report(const T& _t);

Works for me :-)
/conrad.





>
> meeroh
Re: typeid & template func woes
#1489
Author: Howard Hinnant
Date: Wed, 12 Feb 2003 16:47
67 lines
3811 bytes
In article <b2dt3k$2g0n$1@news.dataguard.no>, Conrad Weyns
<weyns@online.no> wrote:

| "Miro Jurisic" <macdev@meeroh.org> skrev i melding
| news:macdev-B2A383.10035512022003@senator-bedfellow.mit.edu...
| > In article <b2dg2g$1117$1@news.dataguard.no>, "Conrad Weyns"
| <weyns@online.no>
| > wrote:
| >
| > > 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:
| > >
| > > 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
| >
| > My understanding is that typeof (T) == typeof (const T) == typeof
| (volatile T)
| > == etc. Perhaps if you explain exactly what you are trying to do with this
| > information we can come up with an answer. (Which will involve type
| traits, I am
| > sure.)
| 
| Thanks meeroh, looks like I need to go back to school -again :-(
| Ehm, I am lying now, I never went to school!
| 
| But this was the kick in the behind that I needed to get to an alternative
| solution. I can simply differentiate between the two cases by supplying
| *two* template functions:
| 
| template <typename T> void _report(T& _t);
| template <typename T> void _report(const T& _t);
| 
| Works for me :-)

Actually the semantics of typeof haven't been worked out by the C++
committee yet.  But what meeroh says is certainly true for our current
implementation of it.

Glad you found a solution!

If it helps any further, there is a neat little type traits (hinted at
by meeroh) that lives in <msl_utility>.  You could have stuck it in
your original _report like so:

    if (Metrowerks::is_const<T>::value)
      cout << "const\n";

And it would have printed "const" for the const variants.

-- 
Howard Hinnant
Metrowerks
Re: typeid & template func woes
#1490
Author: Conrad Weyns
Date: Wed, 12 Feb 2003 20:44
99 lines
4515 bytes
"Howard Hinnant" <hinnant@metrowerks.com> skrev i melding
news:120220031147334006%hinnant@metrowerks.com...
> In article <b2dt3k$2g0n$1@news.dataguard.no>, Conrad Weyns
> <weyns@online.no> wrote:
>
> | "Miro Jurisic" <macdev@meeroh.org> skrev i melding
> | news:macdev-B2A383.10035512022003@senator-bedfellow.mit.edu...
> | > In article <b2dg2g$1117$1@news.dataguard.no>, "Conrad Weyns"
> | <weyns@online.no>
> | > wrote:
> | >
> | > > 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:
> | > >
> | > > 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
> | >
> | > My understanding is that typeof (T) == typeof (const T) == typeof
> | (volatile T)
> | > == etc. Perhaps if you explain exactly what you are trying to do with
this
> | > information we can come up with an answer. (Which will involve type
> | traits, I am
> | > sure.)
> |
> | Thanks meeroh, looks like I need to go back to school -again :-(
> | Ehm, I am lying now, I never went to school!
> |
> | But this was the kick in the behind that I needed to get to an
alternative
> | solution. I can simply differentiate between the two cases by supplying
> | *two* template functions:
> |
> | template <typename T> void _report(T& _t);
> | template <typename T> void _report(const T& _t);
> |
> | Works for me :-)
>
> Actually the semantics of typeof haven't been worked out by the C++
> committee yet.  But what meeroh says is certainly true for our current
> implementation of it.

Yes, I learned the hard way as usual.
Interesting to observe the change in function signature as reported by
__PRETTY_FUNCTION__ after I added the second template function.

With only one function:
     template <typename T> void _report(T& _t)
        _report<long>(long &)
        _report<const long>(const long &)

With an additional function:
    template <typename T> void _report(const T& _t)
        _report<long>(long &)
        _report<long>(const long &)


It's the <long> and <const long> of the first case that got me going on thin
ice...
I must admit, I still don't get it exactly but I something that works!

>
> Glad you found a solution!
>
> If it helps any further, there is a neat little type traits (hinted at
> by meeroh) that lives in <msl_utility>.  You could have stuck it in
> your original _report like so:
>
>     if (Metrowerks::is_const<T>::value)
>       cout << "const\n";
>
> And it would have printed "const" for the const variants.

Thanks Howard. Had a look at that. God d... it, the things I don't know!
Time to throw everything away and start all over again...
Cheers,
Conrad Weyns.

>
> --
> Howard Hinnant
> Metrowerks
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads