On Mon, Feb 9, 2009 at 11:14 PM, Alex Wallisch <alex.w...@gmail.com> wrote:
>
> I am attempting to use google mock to mock a method that takes a
> boost::shared_ptr as an argument.
>
> class Mock : public Interface {
> MOCK_METHOD1( SomeMethod, void(boost::shared_ptr<SomeClass>) );
A side note: it's unnecessarily inefficient to pass a shared_ptr by
value. Try reference instead:
MOCK_METHOD1(SomeMethod, void(const boost::shared_ptr<SomeClass>&));
This shouldn't be related to the errors you saw though.
> };
>
> This generates the error:
>
> /usr/local/include/gmock/gmock-printers.h|189|error: ambiguous
> overload for 'operator<<' in '* os << value'|
> /usr/include/c++/4.3/ostream|177|note: candidates are:
> std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT,
> _Traits>::operator<<(bool) [with _CharT = char, _Traits =
> std::char_traits<char>]|
> /usr/local/include/gmock/gmock-printers.h|120|note:
> std::ostream& testing::internal2::operator<<(std::ostream&, const T&)
> [with T = boost::shared_ptr<SomeClass>]|
> /usr/include/boost/shared_ptr.hpp|515|note:
> std::basic_ostream<E, T>& boost::operator<<(std::basic_ostream<E, T>&,
> const boost::shared_ptr<Y>&) [with E = char, T =
> std::char_traits<char>, Y = SomeClass]|
>
> If it is relevant, I don't have operator<< overloaded for SomeClass.
> I've looked at shared_ptr.hpp and attempted to #define some symbols
> that appear to alter the behavior of operator<< with instances of
> shared_ptr, but doing so did not appear to make any difference in the
> problem. I also tried feeding the error message to gmock_doctor, with
> no luck.
>
> Does anybody have any suggestions as to what the problem might be and
> how I could work around it?
I'll take a closer look to see if we could make the errors go away by
changing GoogleMock. Meanwhile, can you try this workaround described
at the beginning of gmock-printers.h?
// It uses the << operator when possible, and prints the bytes in the
// object otherwise. A user can override its behavior for a class
// type Foo by defining either operator<<(::std::ostream&, const Foo&)
// or void PrintTo(const Foo&, ::std::ostream*) in the namespace that
// defines Foo. If both are defined, PrintTo() takes precedence.
In other words, please keep boost's definition of << unchanged, and
add this in the boost namespace:
template <typename T>
std::ostream& operator<<(std::ostream& os, const boost::shared_ptr<T>& p) {
return os << p.get();
}
I suspect this would work. If it doesn't, please try this instead
(also in the boost namespace):
template <typename T>
void PrintTo(const boost::shared_ptr<T>& p, std::ostream* os) {
*os << p.get();
}
Please let us know if the workaround works. Thanks,
--
Zhanyong
http://codereview.appspot.com/14072
Could you download it, patch GoogleMock, and see if it solves your
problem? (Sorry, I don't have boost shared_ptr on my machine to test
the fix myself.)
Thanks,
--
Zhanyong
Please use patch set #2 as I uploaded the wrong version the first time. Thanks,
--
Zhanyong
--
Zhanyong