25

Have some code:

EXPECT_NE(nullptr,ptr);

And I get the following compilation error:

'operator <<' is ambiguous

could be 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<<void>(std::nullptr_t)'
or       'std::basic_ostream<char,std::char_traits<char>> &testing::internal2::operator <<<char,std::char_traits<char>,T>(std::basic_ostream<char,std::char_traits<char>> &,const T &)'

Could this be a library version problem?

273K
  • 29,503
  • 10
  • 41
  • 64
Igor Pugachev
  • 604
  • 1
  • 5
  • 14

5 Answers5

23

If you want to be more explicit, you could also use

EXPECT_TRUE(ptr != nullptr);

(that's what I normally do)

Btw. funnily enough, in my work project I still have to work with C++98 (still building for Sun and AIX, although it will soon go away) and I ended up creating my own NullPtrT class and NullPtr object in the common library, which actually works with gtest EXPECT_EQ and EXPECT_NE macros. So that I can do

EXPECT_NE(NullPtr, ptr);

I don't remember how exactly I made that work :)

EmDroid
  • 5,918
  • 18
  • 18
  • You might have `operator <<` for `NullPtr` (or the `PrintTo` overload). – Jarod42 Feb 13 '19 at 17:58
  • If you forced to use C++03 or even C++98, you could be interested in CxxComfort (http://ryan.gulix.cl/fossil.cgi/cxxomfort/home) - library intended to support at least some of modern constructions in older versions of the language. It has `nullptr`, BTW. – Andrey Starodubtsev Aug 05 '22 at 16:37
5

I've run into the same problem recently with GTest 1.8.0, but only when using Visual Studio 2019 in C++17 mode. Visual Studio 2019 works fine in C++14 mode, and neither Clang nor GCC seem to have the same problem in C++17 mode.

The issue is that with C++17 there's a new overload in the standard library for the std::ostream::operator<< that takes a nullptr_t, but GTest also provides its own, so your compiler does not know which one to use.

If you have full control over your version of GTest then https://github.com/google/googletest/pull/1620/commits/f66ab00704cd47e4e63ef6d425ca14b9192aaebb is a change for GTest-1.8.0 that resolves the issue: It's not as easy as deleting the overload, because the function in question is a template whose other instantiations are still used. Instead, the solution is to define an explicit void PrintTo(std::nullptr_t, ::std::ostream* os) function that will then automatically be used, no longer deferring to the ambiguous overloads.

When modifying GTest is not an option then the solutions mentioned in the other answers to not use EXPECT_EQ/EXPECT_NE when one parameter is a nullptr_t are your best bet.

Dreamer
  • 1,139
  • 9
  • 18
5
#include "gtest.h"

using ::testing::NotNull;

ASSERT_THAT(ptr, NotNull());

This will give you some more descriptive errors and keeps you using the existing framework. Other benefits are compatibility with smart pointers and raw pointers.

Other matchers can be found on the gtest matchers documentation.

Alex Davis
  • 51
  • 1
  • 2
2
namespace {
  template<class T>
  auto not_nullptr(T*p) -> testing::AssertionResult
  {
    if (p)
      return testing::AssertionSuccess();
    else
      return testing::AssertionFailure() << "pointer is null";
  }
}

...

EXPECT_TRUE(not_nullptr(ptr));

reference:

https://github.com/google/googletest/blob/master/docs/advanced.md#using-a-function-that-returns-an-assertionresult

DerKasper
  • 167
  • 2
  • 11
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
2

Google Test documentation says that,

When comparing a pointer to NULL, use EXPECT_EQ(ptr, nullptr) instead of EXPECT_EQ(ptr, NULL)

When comparing a pointer to NULL, use EXPECT_NE(ptr, nullptr) instead of EXPECT_NE(ptr, NULL).

Therefore, you can just use EXPECT_NE(ptr, nullptr);.

Read more: https://google.github.io/googletest/reference/assertions.html#EXPECT_NE

Tharindu Sathischandra
  • 1,654
  • 1
  • 15
  • 37