[Insight-developers] templates and friends
Luis Ibanez
ibanez at cs.unc.edu
Fri Jun 16 09:13:47 EDT 2000
Hi,
There is a potential conflict with the use
of friend function in templated classes.
It happens with code like this
---------------------------------------
template <class T>
class myClass {
public:
friend myFunction( myClass<T> );
};
---------------------------------------
(This is usually done for ostream & operator<<
and istream & operator>>)
Currently we have this conflict in itkIndex.h (line 122)
The friend function is implicitly templated because
it receives a templated class as parameter.
Compilers have different reactions to this.
Visual C++ accept it as this, and interpret the
implicit parametrization of the function.
Gcc on the other hand, raise a warning because
it expects to find a explicit indication that the
function is templated. This should take the form of
friend myFunction <> (myClass<T>);
the "<>" indicating that the function is templated.
----
This have been solved in VXL code by using a macro
named
VCL_STL_NULL_TMPL_ARGS
(ie, in vxl/vnl_matrix.h lines 136-146,
and in vxl/vnl_vector.h lines 117-137 )
This macro is defined as "<>" or as "/* <> */
depending on the compiler, in the header file
vxl/vcl/vcl_compiler.h
------
Other options are just to turn off the warning in
gcc by using
-Wno-non-template-friend
or using the gcc compiler option
-fguiding-decls
as is done in vxl makefiles.
------
What are your preferences about the
way in which that should be done ?
Luis
More information about the Insight-developers
mailing list