Topic: Template function overloads
Author: "Sam Lindley" <sam@redsnapper.net>
Date: 1999/10/22 Raw View
> Results with Microsoft Visual C++ 6.0 and GNU 2.95.1
> int: 1
> unsigned int: 2
> unsigned long: 3
Oops (you made a typo).
They both give:
int: 0
unsigned int: 1
unsigned long: 2
double: 3
as they should.
Sam Lindley
---
[ 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: "Stevan Warwick" <swarwick@starbase.com>
Date: 1999/10/18 Raw View
From: Stevan Warwick <swarwick@starbase.com>
Subject: Template function overloads with Sun Workshop 5.0
Date: Thursday, October 14, 1999 10:48 AM
Hi,
I have found a problem with Sun Workshop 5.0 C++ compiler(All relevant
patches were installed). Apparently, they do not resolve template function
overloads properly. The following code produces:
// TemplateTest.cpp : Defines the entry point for the console application.
//
#include <stdio.h>
template<class T> int TemplateFunction( T arg )
{ return (int)0; }
template<> int TemplateFunction<const unsigned int&>( const unsigned int& )
{ return 1; }
template<> int TemplateFunction<const unsigned long&>( const unsigned
long& )
{ return 2; }
template<> int TemplateFunction<const double&>( const double& )
{ return 3; }
template<class T> class TestClass
{
public:
int ClassMethod( const T& arg );
};
template<class T>
int TestClass<T>::ClassMethod( const T& arg )
{
return TemplateFunction<const T&>( arg );
}
void TemplateTest()
{
int int_arg = 0;
TestClass<int> Test_int;
wprintf( L"int: %d\n", Test_int.ClassMethod( int_arg ) );
unsigned int uint_arg = 0;
TestClass<unsigned int> Test_uint;
wprintf( L"unsigned int: %d\n", Test_uint.ClassMethod( uint_arg ) );
unsigned long ulong_arg = 0;
TestClass<unsigned long> Test_ulong;
wprintf( L"unsigned long: %d\n", Test_ulong.ClassMethod( ulong_arg ) );
double dbl_arg = 0;
TestClass<double> Test_dbl;
wprintf( L"double: %d\n", Test_dbl.ClassMethod( dbl_arg ) );
}
int main(int argc, char* argv[])
{
TemplateTest();
return 0;
}
Results on Sun Workshop 5.0 C++
int: 0
unsigned int: 0
unsigned long: 0
Results with Microsoft Visual C++ 6.0 and GNU 2.95.1
int: 1
unsigned int: 2
unsigned long: 3
I've reported this to sun and there asking me to point them to relevant ANSI
documentation stating that function templates should operate this way. Can
anyone point me to ANSI document which describes this?
Thanks,
Steve
Starbase Corporation
swarwick@starbase.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: "Ron" <nospam@this-address.org>
Date: 1999/10/21 Raw View
Stevan Warwick <swarwick@starbase.com> wrote in message
news:s0c866dchu215@news.supernews.com...
> Results on Sun Workshop 5.0 C++
> int: 0
> unsigned int: 0
> unsigned long: 0
>
> Results with Microsoft Visual C++ 6.0 and GNU 2.95.1
> int: 1
> unsigned int: 2
> unsigned long: 3
Results from BCB4 with patch 1:
int: 0
unsigned int: 1
unsigned long: 2
double: 3
Why don't your results include an entry for double?
-- Ron
---
[ 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: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/21 Raw View
"Stevan Warwick" <swarwick@starbase.com> writes:
> I have found a problem with Sun Workshop 5.0 C++ compiler(All relevant
>patches were installed). Apparently, they do not resolve template function
>overloads properly. The following code produces:
The code uses explicit template arguments on template functions.
That feature, as explained in the compiler documentation, is not
supported by C++ 5.0.
--
Steve Clamage, stephen.clamage@sun.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: b_thomas@ibm.net
Date: 1999/10/21 Raw View
In article <s0c866dchu215@news.supernews.com>,
"Stevan Warwick" <swarwick@starbase.com> wrote:
>
> I have found a problem with Sun Workshop 5.0
C++ compiler(All relevant
> patches were installed). Apparently, they do not
resolve template function
> overloads properly. The following code produces:
>
> template<class T> int TemplateFunction( T arg )
> { return (int)0; }
>
> template<> int TemplateFunction<const unsigned
int&>( const unsigned int& )
> { return 1; }
>
> template<> int TemplateFunction<const unsigned
long&>( const unsigned
> long& )
> { return 2; }
>
> template<> int TemplateFunction<const double&>(
const double& )
> { return 3; }
>
> template<class T> class TestClass
> {
> public:
> int ClassMethod( const T& arg );
> };
>
> template<class T>
> int TestClass<T>::ClassMethod( const T& arg )
> {
> return TemplateFunction<const T&>( arg );
> }
>
> void TemplateTest()
> {
> int int_arg = 0;
> TestClass<int> Test_int;
> wprintf( L"int: %d\n", Test_int.ClassMethod(
int_arg ) );
>
> unsigned int uint_arg = 0;
> TestClass<unsigned int> Test_uint;
> wprintf( L"unsigned int: %d\n",
Test_uint.ClassMethod( uint_arg ) );
>
> unsigned long ulong_arg = 0;
> TestClass<unsigned long> Test_ulong;
> wprintf( L"unsigned long: %d\n",
Test_ulong.ClassMethod( ulong_arg ) );
>
> double dbl_arg = 0;
> TestClass<double> Test_dbl;
> wprintf( L"double: %d\n", Test_dbl.ClassMethod(
dbl_arg ) );
> }
>
> int main(int argc, char* argv[])
> {
> TemplateTest();
> return 0;
> }
>
> Results on Sun Workshop 5.0 C++
> int: 0
> unsigned int: 0
> unsigned long: 0
>
> Results with Microsoft Visual C++ 6.0 and GNU
2.95.1
> int: 1
> unsigned int: 2
> unsigned long: 3
>
Interesting. Another compiler (VisualAge C++ 4.0)
produces:
int: 0
unsigned int: 1
unsigned long: 2
double: 3
I think this last one is correct.
> I've reported this to sun and there asking me to
point them to relevant ANSI
> documentation stating that function templates
should operate this way. Can
> anyone point me to ANSI document which describes
this?
>
I am not sure what exactly they are asking for.
Explicit specialization (including function
templates) is covered in section 14.7.3 of the
standard.
--
Biju Thomas
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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: "Sam Lindley" <sam@redsnapper.net>
Date: 1999/10/21 Raw View
> I have found a problem with Sun Workshop 5.0 C++ compiler(All relevant
> patches were installed). Apparently, they do not resolve template function
> overloads properly. The following code produces:
>
> // TemplateTest.cpp : Defines the entry point for the console application.
> //
>
> #include <stdio.h>
>
> template<class T> int TemplateFunction( T arg )
> { return (int)0; }
>
> template<> int TemplateFunction<const unsigned int&>( const unsigned
int& )
> { return 1; }
>
> template<> int TemplateFunction<const unsigned long&>( const unsigned
> long& )
> { return 2; }
>
> template<> int TemplateFunction<const double&>( const double& )
> { return 3; }
>
> template<class T> class TestClass
> {
> public:
> int ClassMethod( const T& arg );
> };
>
> template<class T>
> int TestClass<T>::ClassMethod( const T& arg )
> {
> return TemplateFunction<const T&>( arg );
> }
>
> void TemplateTest()
> {
> int int_arg = 0;
> TestClass<int> Test_int;
> wprintf( L"int: %d\n", Test_int.ClassMethod( int_arg ) );
>
> unsigned int uint_arg = 0;
> TestClass<unsigned int> Test_uint;
> wprintf( L"unsigned int: %d\n", Test_uint.ClassMethod( uint_arg ) );
>
> unsigned long ulong_arg = 0;
> TestClass<unsigned long> Test_ulong;
> wprintf( L"unsigned long: %d\n",
Test_ulong.ClassMethod( ulong_arg ) );
>
> double dbl_arg = 0;
> TestClass<double> Test_dbl;
> wprintf( L"double: %d\n", Test_dbl.ClassMethod( dbl_arg ) );
> }
>
> int main(int argc, char* argv[])
> {
> TemplateTest();
> return 0;
> }
>
> Results on Sun Workshop 5.0 C++
> int: 0
> unsigned int: 0
> unsigned long: 0
>
> Results with Microsoft Visual C++ 6.0 and GNU 2.95.1
> int: 1
> unsigned int: 2
> unsigned long: 3
>
> I've reported this to sun and there asking me to point them to relevant
ANSI
> documentation stating that function templates should operate this way. Can
> anyone point me to ANSI document which describes this?
The whole of section 14 (templates) of the standard (ISO IEC 14882:1998) is
relevant, in particular sections 14.7 and 14.8. But, if Sun really needs
this pointing out to them, then there's something seriously wrong.
Sun's own documentation ('/opt/SUNWspro/READMEs/c++') admits that it doesn't
support your code (though I'm surprised you don't get a diagnostic):
"
0. Standards
=============
The C++ compiler (CC) supports the ISO standard for C++,
ISO IS 14882:1998, Programming Language C++. The following list
describes requirements in the standard not supported in this release:
o new syntax for function templates: f<mytype>(args) <-----
o non-type template parameters for function templates
o class member templates
o partial specialization of templates
o template template parameters (templates as template parameters)
o universal character names
"
(Of course there are many other problems which aren't mentioned.)
If a high level of compliance to the standard under Solaris is important to
you, then Sun's compiler is not your best choice. You'll do much better with
something like gcc 2.95.1 or KAI C++. I wish Sun would get there act
together soon!
Sam Lindley
---
[ 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 ]