Compare ptr with nullptr in gtest
Asked Answered
C

5

26

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?

Chanticleer answered 13/2, 2019 at 16:27 Comment(9)
Try another order.Unworthy
Same, not workingChanticleer
Why not just EXPECT_TRUE(ptr);?Fanny
An editorial note: Error messages that contain the > character render horribly when you put them in blockquotes. Better indent 4 spaces and render as code. I'd edit myself, but the mobile UI makes this tricky.Cryptogram
github.com/google/googletest/blob/master/googletest/docs/… -> search for EXPECT_NE and read the first paragraphs for some backgroundHistrionics
Are you applying using namespace std; somewhere?Unworthy
EXPECT_TRUE(ptr); works. Thank you!Chanticleer
github.com/google/googletest/blob/master/googletest/docs/…Histrionics
To put it simply, that's how C++ is. New features are added continuously (like nullptr), and they interact with the old with the old features... in an unpredictable way.Hypergolic
V
24

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 :)

Vlaminck answered 13/2, 2019 at 17:0 Comment(2)
You might have operator << for NullPtr (or the PrintTo overload).Platinotype
If you forced to use C++03 or even C++98, you could be interested in CxxComfort (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.Tavey
S
9
#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.

Sporran answered 24/10, 2022 at 16:38 Comment(2)
Thanks! Seams to be the best solution for me. Worth to mention that in the context of op it should be EXPECT_THAT.Pigmentation
This should be top answerPuerilism
H
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.

Hassett answered 16/1, 2020 at 9:4 Comment(1)
The PR mentioned in answer is already in 'merged' state, so I have no problems with MSVC 2019 and latest at the moment gTest 1.12.1Tavey
S
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

Seasoning answered 19/8, 2022 at 6:19 Comment(0)
P
1
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

Pallor answered 13/2, 2019 at 16:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.