Re: Avoid typename repetition in template specialization

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Frank,
On Fri, Apr 09, 2010 at 07:11:04AM -0700, John (Eljay) Love-Jensen wrote:
> 
> I would use this approach:
> typedef PScalar< PColorMatrix< RComplex<REAL64>, Nc> > TScalarMatrix;
> typedef PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > TSpinVector;
> template<>
> inline BinaryReturn< TScalarMatrix , TSpinVector , OpMultiply >::Type_t
> operator+(const T1& l, const T2& r )
> {
>    BinaryReturn< TScalarMatrix , TSpinVector , OpMultiply >::Type_t d;
>     // code
>    return d;
> }
> 
> Although TScalarMatrix is probably more semantic detail (and longer to
> type) than you'd prefer over T1, it avoids the naming collision.

Maybe another solution (depending on your code - maybe the best is to
combine Eljay's suggestions of "understandable" typedefs with it) would
be to introduce additional, artifical namespaces - for each template
function one. This gives you a means to do "local typedefs":

namespace NS1{
  typedef PScalar< PColorMatrix< RComplex<REAL64>, Nc> > T1;
  typedef PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > T2;
  typedef OpMultiply Op;
  template<>
  inline BinaryReturn< T1 , T2 , Op >::Type_t
  operator*(const T1& l, const T2& r )
  {
    BinaryReturn< T1 , T2 , Op >::Type_t d;
    // code
    return d;
  }
}
using NS1::operator*;
namespace NS2{
  typedef PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > T1;
  typedef PSpinVector< PColorVector< RComplex<REAL64>, Nc>, Ns > T2;
  typedef OpMultiply Op;
  template<>
  inline BinaryReturn< T1 , T2 , Op >::Type_t
  operator+(const T1& l, const T2& r )
  {
    BinaryReturn< T1 , T2 , Op >::Type_t d;
        // code
    return d;
  }
}

using NS2::operator+;

Then you have no conflicts between the two T1's, as they are definied in
different namespaces - this code should compile fine, I believe.
However, this might render your code (and for sure: the error-messages
from the compiler;-)) more difficult to understand - and there are some
additional constraints, e.g. all template spezializations of a function
have to be in the same namespace:
namespace NS3{
  template <typename T> void Function(const T & x)
  {
     //code
  }
  typedef int T1;
  template <> void Function<T1>(const T1 & x)
  {
    //code
  }
}
using NS3::Function;

However, normal "overloading" can be done between two different
namespaces:
namespace NS4{
  void Function()
  {
    //code
  }
}
using NS4::Function;

should work.

HTH,

Axel

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux