Here's a question that has me stumped at the moment: I am trying to define a humble operator+ in a templated vector class, but the syntax seems to be extremely compiler-dependent. The operator is defined as a friend, because the first operand is not of the class type (I am trying to define a vector = scalar+vector operation). This is what I've got at the moment:
Ok, so it's ugly, but this was the only way to make it work with all the compilers I've had in use so far. No single syntax seems to agree with all of them.
However, now I also need to compile the code for CUDA, and the nvcc compiler doesn't seem to understand any of the above definitions. I'm stuck. Anybody came across this problem before, or has a suggestion for a workaround? What is the "official" syntax for this kind of definition? (So far I've simply been experimenting until a compiler swallowed the syntax I've offered).
Code:
template<class VT> class TVector;
template<class VT>
TVector<VT> operator+ (const VT &s, const TVector<VT> &v);
template<class VT> class TVector {
public:
...
#if (defined(WIN32)||defined(WIN64))
template <class VT> friend MATHLIB TVector<VT> operator+ (const VT &s,
const TVector<VT> &v);
#elif (GCC_VERSION < 30404) // old-style definitions
friend TVector<VT> operator+ <> (const VT &s, const TVector<VT> &v);
#else
friend TVector<VT> (::operator+ <>) (const VT &s, const TVector<VT> &v);
#endif
...
};
However, now I also need to compile the code for CUDA, and the nvcc compiler doesn't seem to understand any of the above definitions. I'm stuck. Anybody came across this problem before, or has a suggestion for a workaround? What is the "official" syntax for this kind of definition? (So far I've simply been experimenting until a compiler swallowed the syntax I've offered).