[Insight-developers] templates and friends

Bill Hoffman bill.hoffman at kitware.com
Fri Jun 16 10:56:04 EDT 2000


I think we might be able to dodge this issue altogether.
The only place this is commonly used is with the operator<<.
Since all classes must have a virtual Print function that is
public, we could just call it from a non-friend function.
In fact, the only class that needs to implement the operator<<
is itkObject as it can call the virtual Print function.


For all classes that are not sub-classes of itkObject, we could
mandate that a public Print(std::ostream&) function is provided.
Then we could declare a templated operater<< in itkObject that
works for all the classes in itk, both sub-classes and non-subclasses
of itkObject.  It would not have to be a friend to anyone, as
it is only calling a public Print function.




At 09:13 AM 6/16/00 -0400, Luis Ibanez wrote:
>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
>
>
>_______________________________________________
>Insight-developers mailing list
>Insight-developers at public.kitware.com
>http://public.kitware.com/mailman/listinfo/insight-developers 





More information about the Insight-developers mailing list