Topic: Q: Instantiating function templates
Author: "Scott Robert Ladd" <sladd@tampabay.rr.com>
Date: 2000/06/25 Raw View
Hi,
I have a piece of code that *I* believe conforms to Standard C++ -- but it
doesn't work, so either my compiler is confused, or I am. The problem is
demonstrated by the following program:
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
template <class T> void showType()
{
cout << typeid(T).name() << endl;
};
template <class T> class SomeThing
{
public:
void showTypeInfo()
{
showType<T>();
}
};
void main()
{
SomeThing<int> st_int;
st_int.showTypeInfo();
SomeThing<string> st_string;
st_string.showTypeInfo();
SomeThing<short> st_short;
st_short.showTypeInfo();
}
The above displays:
short
short
short
Which is *not* what I want, obviously. I want to see:
int
string
short
Now, as I understand it, by explicitly stating showType<int>, I am telling
the compiler to create a specialization of the showType function template
for type int.
I have reread the Standard, and still see no reason why the program above
should not behave as I expect. Am I embarassing myself by missing something
obvious? Or is this not allowed under Standard C++?
--
** Scott Robert Ladd
Coyote Gulch Productions - http://www.coyotegulch.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: comeau@panix.com (Greg Comeau)
Date: 2000/06/25 Raw View
In article <cU255.14336$ez6.58996@typhoon.tampabay.rr.com>,
Scott Robert Ladd <sladd@tampabay.rr.com> wrote:
>I have a piece of code that *I* believe conforms to Standard C++ -- but it
>doesn't work, so either my compiler is confused, or I am. The problem is
>demonstrated by the following program:
>
>#include <string>
>#include <iostream>
>#include <iomanip>
>
>using namespace std;
>
>template <class T> void showType()
>{
> cout << typeid(T).name() << endl;
>};
A ; is not allowed here.
>template <class T> class SomeThing
>{
>public:
> void showTypeInfo()
> {
> showType<T>();
> }
>};
>
>void main()
This is best avoided. For why, check out
http://www.comeaucomputing.com/techtalk/#voidmain.
>{
> SomeThing<int> st_int;
> st_int.showTypeInfo();
>
> SomeThing<string> st_string;
> st_string.showTypeInfo();
>
> SomeThing<short> st_short;
> st_short.showTypeInfo();
>}
>
>The above displays:
>
>short
>short
>short
>
>Which is *not* what I want, obviously. I want to see:
>
>int
>string
>short
>
>Now, as I understand it, by explicitly stating showType<int>, I am telling
>the compiler to create a specialization of the showType function template
>for type int.
>
>I have reread the Standard, and still see no reason why the program above
>should not behave as I expect. Am I embarassing myself by missing something
>obvious? Or is this not allowed under Standard C++?
Here's the results using Comeau C++ on a SPARC:
int
std::basic_string<char, std::char_traits<char>, std::allocator<char>>
short
- Greg
--
Comeau Computing / Comeau C/C++ 4.2.42 (4.2.44 expected soon)
TRY Comeau C++ ONLINE at http://www.comeaucomputing.com/tryitout
Email: comeau@comeaucomputing.com / WEB: http://www.comeaucomputing.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Steven King <sxking@uswest.net>
Date: 2000/06/26 Raw View
Greg Comeau wrote:
>
> In article <cU255.14336$ez6.58996@typhoon.tampabay.rr.com>,
> Scott Robert Ladd <sladd@tampabay.rr.com> wrote:
> >I have a piece of code that *I* believe conforms to Standard C++ -- but it
> >doesn't work, so either my compiler is confused, or I am. The problem is
> >demonstrated by the following program:
> >
> >#include <string>
> >#include <iostream>
> >#include <iomanip>
> >
> >using namespace std;
> >
> >template <class T> void showType()
> >{
> > cout << typeid(T).name() << endl;
> >};
this is ill-formed -- using typeid without
#include <typeinfo> (5.2.8.6)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 2000/06/26 Raw View
Scott Robert Ladd wrote:
>
> Hi,
>
> I have a piece of code that *I* believe conforms to Standard C++ -- but it
> doesn't work, so either my compiler is confused, or I am. The problem is
> demonstrated by the following program:
>
> #include <string>
> #include <iostream>
> #include <iomanip>
>
> using namespace std;
>
> template <class T> void showType()
> {
> cout << typeid(T).name() << endl;
> };
>
> template <class T> class SomeThing
> {
> public:
> void showTypeInfo()
> {
> showType<T>();
> }
> };
>
> void main()
> {
> SomeThing<int> st_int;
> st_int.showTypeInfo();
>
> SomeThing<string> st_string;
> st_string.showTypeInfo();
>
> SomeThing<short> st_short;
> st_short.showTypeInfo();
> }
>
> The above displays:
>
> short
> short
> short
>
> Which is *not* what I want, obviously. I want to see:
>
> int
> string
> short
Keep in mind that std::typeinfo::name() returns an
implementation-defined string. The string's not guaranteed to be useful
for any purpose. In particular, it's not guaranteed to be a different
string for different types. I can't even find anything that says it
should return equivalent strings for identical types.
Note also that std::typeinfo::name() necessarily depends upon the actual
type. 'string' is merely a typedef for a template class with one
explicit and two default arguments. The name returned would likely be
the same as the one that would be returned by referring to that template
explicitly, with all of the arguments explicitly stated. You'd most
likely get something like "std::basic_string<char,
std::char_traits<char>, std::allocator<char> >", rather than "string".
....
> I have reread the Standard, and still see no reason why the program above
> should not behave as I expect. Am I embarassing myself by missing something
> obvious? Or is this not allowed under Standard C++?
Ignoring all my nit-picking caveats, I don't see any good reason for you
to have gotten the results you've shown.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Scott Robert Ladd" <sladd@tampabay.rr.com>
Date: 2000/06/26 Raw View
> Here's the results using Comeau C++ on a SPARC:
> int
> std::basic_string<char, std::char_traits<char>, std::allocator<char>>
> short
Okay, that's what I expected, so I gather that Visual C++ is broken in yet
another fashion. Growl... I'll have to fire up the Linux box and see if the
code works with gcc 2.95.2.
As for "void main" -- yeah, my mistake. Should be int main(). But for a
trivial "show the bug" app, it was a non sequitor...
--
** Scott Robert Ladd
Coyote Gulch Productions - http://www.coyotegulch.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: hinnant@anti-spam_metrowerks.com (Howard Hinnant)
Date: 2000/06/26 Raw View
In article <8j2ks2$b06$1@panix6.panix.com>, comeau@comeaucomputing.com wrote:
| Here's the results using Comeau C++ on a SPARC:
| int
| std::basic_string<char, std::char_traits<char>, std::allocator<char>>
| short
Same for Metrowerks CodeWarrior.
-Howard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]