A
aaragon
Hi... after trying several things with some templated code I decided
to ask the experts a couple of things. I have the following class
template:
template<class A>
class Expr {
A a_;
public:
... // some public functions
...
friend inline std:stream& operator<<(std:stream& os, const
Expr<A>& e) {
os<<e.a_();
return os;
}
};
operator<< works just fine, but then I wanted to have the operator<<
with different behaviors depending on the template parameter. So, I
tried the following inside the class:
// declaration inside the class
friend std:stream& operator<<<A>(std:stream& os, const Expr&
e);
and defining the function outside:
template <class A>
inline std:stream& operator<<(std:stream& os, const Expr<A>&
e) {
os<<e();
return os;
}
Then I got just too many errors because of ambiguous calls and others
like:
cppblas.hpp:862: error: template-id 'operator<< <UnOp<Vector<double>,
TrOp> >' for 'std::basic_ostream<char, std::char_traits<char> >&
operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
Expr<UnOp<Vector<double>, TrOp> >&)' does not match any template
declaration
So then, I decided to forward the function call to another function,
say print:
template <class A>
inline std:stream& print(std:stream& os, const Expr<A>& e) {
os<<e();
return os;
}
so this also works fine, and now I can specialize the function for
specific class of A:
template <>
inline std:stream& print<someClass>(std:stream& os, const
Expr<someClass>& e) {
// partially specialized for someClass
return os;
}
Now the questions is, is this the better way to do this? Forwarding a
function call seems right, but probably it can be done without another
function. I tried hard overloading operator<< but no success. Any
hints??
Thank you all...
a²
to ask the experts a couple of things. I have the following class
template:
template<class A>
class Expr {
A a_;
public:
... // some public functions
...
friend inline std:stream& operator<<(std:stream& os, const
Expr<A>& e) {
os<<e.a_();
return os;
}
};
operator<< works just fine, but then I wanted to have the operator<<
with different behaviors depending on the template parameter. So, I
tried the following inside the class:
// declaration inside the class
friend std:stream& operator<<<A>(std:stream& os, const Expr&
e);
and defining the function outside:
template <class A>
inline std:stream& operator<<(std:stream& os, const Expr<A>&
e) {
os<<e();
return os;
}
Then I got just too many errors because of ambiguous calls and others
like:
cppblas.hpp:862: error: template-id 'operator<< <UnOp<Vector<double>,
TrOp> >' for 'std::basic_ostream<char, std::char_traits<char> >&
operator<<(std::basic_ostream<char, std::char_traits<char> >&, const
Expr<UnOp<Vector<double>, TrOp> >&)' does not match any template
declaration
So then, I decided to forward the function call to another function,
say print:
template <class A>
inline std:stream& print(std:stream& os, const Expr<A>& e) {
os<<e();
return os;
}
so this also works fine, and now I can specialize the function for
specific class of A:
template <>
inline std:stream& print<someClass>(std:stream& os, const
Expr<someClass>& e) {
// partially specialized for someClass
return os;
}
Now the questions is, is this the better way to do this? Forwarding a
function call seems right, but probably it can be done without another
function. I tried hard overloading operator<< but no success. Any
hints??
Thank you all...
a²