Topic: Template specialization trouble
Author: "Sebastian Moleski" <sebmol@gmx.net>
Date: 2000/08/03 Raw View
"Steven E. Harris" <sharris@nospam.primus.com>:
| We're trying to introduce a way to extract a computed value (here
| called 'foo') from any compliant type. For built-in types, we have
| special implementations. For non-anticipated user-defined types, we'd
| like to call a known member function. This known member function could
| be incorporated by derivation from a mixin base class, but we're
| striving to avoid the virtual call overhead and the invasive nature of
| the derivation.
...
|
| int foo(int val)
| { return val; }
|
...
|
| Note the attempt to handle a user-defined type by calling a member
| function 'foo,' the attempt to handle any pointer by casting the
| value, and special handling of type int. The strategy used for int
| would also be used for other built-in types such as double.
"int foo(int)" is not a specialization for your template function but a separate
function apart from your templates which happens to have the same name.
"template<> foo(int)" is the correct specialization.
| Should this code compile? Our available compilers don't agree:
| o MSVC 6.0sp4 Y
| o Sun C++ 5.0 Y
| o Comeau C++ 4.2.43 BETA#1 Y
| o Gnu g++ 2.95.2 N
o Borland C++ 5.5 Y
| If we uncomment the commented-out function, *none* of the four
| compilers mention will accept the code.
Borland C++ does.
Also, it is not necessary to have two template functions, one requesting a const
pointer and the other a non-const one:
template <typename T> int foo(T *val);
template<typename T> int foo(const T *val);
Normal, non-const pointer can be converted into const pointers implicitely.
Since constness does not affect the inner workings of your template functions,
the const version should suffice.
Regards,
Sebastian Moleski
---
[ 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: sharris@nospam.primus.com (Steven E. Harris)
Date: 2000/08/02 Raw View
We're trying to introduce a way to extract a computed value (here
called 'foo') from any compliant type. For built-in types, we have
special implementations. For non-anticipated user-defined types, we'd
like to call a known member function. This known member function could
be incorporated by derivation from a mixin base class, but we're
striving to avoid the virtual call overhead and the invasive nature of
the derivation.
Our attempt takes this form:
--- Cut Here ---
#include <iostream>
template <typename T>
int foo(const T& val)
{ return val.foo(); }
template <typename T>
int foo(T *val)
{ return reinterpret_cast<int>(val); }
// [see note below]
//
// template<typename T>
// int foo(const T *val)
// { return reinterpret_cast<int>(val); }
int foo(int val)
{ return val; }
int main(int,char **)
{
const int i = 5;
std::cerr << "i = " << foo(i) << '\n';
std::cerr << "&i = " << foo(&i) << '\n';
return 0;
}
--- Cut Here ---
Note the attempt to handle a user-defined type by calling a member
function 'foo,' the attempt to handle any pointer by casting the
value, and special handling of type int. The strategy used for int
would also be used for other built-in types such as double.
Should this code compile? Our available compilers don't agree:
o MSVC 6.0sp4 Y
o Sun C++ 5.0 Y
o Comeau C++ 4.2.43 BETA#1 Y
o Gnu g++ 2.95.2 N
If we uncomment the commented-out function, *none* of the four
compilers mention will accept the code.
It seems like this facility should be possible to write with the right
combination of template functions, templated functors, and maybe even
some of the boost::call_traits magic. Does our example above
incorporate the proper template facilities to get this job done? Any
corrections and suggestions would be most appreciated.
--
Steven E. Harris
Primus Knowledge Solutions, Inc.
http://www.primus.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 ]