I want to do something like this:
#include <iostream>
template<typename T>
class myclass
{
T something;
public:
myclass(T something) : something{ something }
{ }
struct result
{
T value;
};
result get_result()
{
return{ something };
}
};
template<typename T>
std::ostream& operator<<(std::ostream& stream, const typename myclass<T>::result& res)
{
return stream << res.value;
}
int main()
{
myclass<int> m(0);
std::cout << m.get_result() << "\n";
}
In this case neither gcc nor msvc find the overloaded stream operator when I put in a result (which is dependent on a templated outer class). Is what I am trying to do even possible?
T
is non-deductible for myclass<T>::result
.
You may define operator<<
inside the class to solve your issue:
struct result
{
T value;
friend std::ostream& operator<<(std::ostream& stream, const result& res)
{
return stream << res.value;
}
};
Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With