Re: why is operator<< not found
homsan toft wrote:
I've tried the below code with MSVC and Comeau online compiler.
Both complain that operator<< for Outer<part<size_t> >::inner is not
defined. So how do I declare it without doing full specialization?
I've tried a template on any type, like Outer<AnyT>::inner (see
below) Also tried a template on the templated type: Outer<part<X>
>::inner
// (see below) I can't find a typo? What is the rule in operation here?
Thanks,
homsan
---------------- 8< -----------------
#include <iostream>
template<class SizeT>
struct part {
typedef SizeT size_type;
size_type size;
};
template<class PartT>
class Outer
{
public:
typedef PartT part_type;
struct inner {
part_type p;
};
inner get() const { return inner(); }
};
// WHY is this not found in main()?
This is "non-deducible context". You're not using 'PartT' here.
Why do you think you need it? Why couldn't you use
template<class Blah> ..., Blah const& it)
???
template<class PartT>
inline std::ostream& operator<<(std::ostream& ostr, typename
Outer<PartT>::inner const& it) {
ostr << it.p.size << " by Jove\n";
return ostr;
}
// Nor is this found - what gives?
It's called "non-deducible context". Why do you need to know 'SizeT'
anyway?
You're not using it. Why could't you simply use OuterInner?
template<class SizeT>
inline std::ostream& operator<<(std::ostream& ostr, typename
Outer<part<SizeT> >::inner const& it) {
ostr << it.p.size << " by Jove\n";
return ostr;
}
// Now this works, but of course I don't want to redefine for every
specialization... /*
std::ostream& operator<<(std::ostream& ostr, Outer<part<size_t>
::inner const& it) {
ostr << it.p.size << " by Jove\n";
return ostr;
}
*/
int main() {
Outer<part<size_t> > yippie;
std::cout << yippie.get();
return 0;
}
--
Please remove capital As from my address when replying by mail