Using google mock with boost::shared_ptr

829 views
Skip to first unread message

Alex Wallisch

unread,
Feb 10, 2009, 2:14:48 AM2/10/09
to Google C++ Mocking Framework
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>) );
};

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?

Zhanyong Wan (λx.x x)

unread,
Feb 10, 2009, 1:11:20 PM2/10/09
to Alex Wallisch, Google C++ Mocking Framework
Hi Alex,

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

Zhanyong Wan (λx.x x)

unread,
Feb 10, 2009, 2:23:09 PM2/10/09
to Alex Wallisch, Google C++ Mocking Framework
I think I have found the problem. I have uploaded a patch to

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

Zhanyong Wan (λx.x x)

unread,
Feb 10, 2009, 2:28:27 PM2/10/09
to Alex Wallisch, Google C++ Mocking Framework
On Tue, Feb 10, 2009 at 11:23 AM, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
> I think I have found the problem. I have uploaded a patch to
>
> 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.)

Please use patch set #2 as I uploaded the wrong version the first time. Thanks,

--
Zhanyong

Alex Wallisch

unread,
Feb 11, 2009, 3:28:52 AM2/11/09
to Google C++ Mocking Framework
Yes, this works. Thanks muchly!

Alex

On Feb 10, 11:28 am, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
> On Tue, Feb 10, 2009 at 11:23 AM, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
>
> > I think I have found the problem.  I have uploaded a patch to
>
> >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.)
>
> Please use patch set #2 as I uploaded the wrong version the first time.  Thanks,
>
>
>
>
>
>
>
> > Thanks,
>
> > On Tue, Feb 10, 2009 at 10:11 AM, Zhanyong Wan (λx.x x) <w...@google.com> wrote:
> >> Hi Alex,
>

Zhanyong Wan (λx.x x)

unread,
Feb 11, 2009, 3:54:59 AM2/11/09
to Alex Wallisch, Google C++ Mocking Framework
Thanks for confirming! The fix will be in the next release of Google Mock.

--
Zhanyong

Reply all
Reply to author
Forward
0 new messages