Gmock and forward declared classes

Suppose I have this class and the type Manager is declared forward in Base.h.

#include <Base.h>

class MockBase : public Base
{
public:
    MOCK_CONST_METHOD0( manager, const Manager&( ) );
    ...
};

I will not use this method in my test, so I do not want to include the definition of the Manager class in the test file.

But I think that when compiling gmock tries to prepare an error message and deep in its gut, it accepts the address of the Manager variable, and I have an error:

error C2027: using undefined type 'Manager' \ external \ googlemock \ gtest \ include \ gtest \ gtest-printers.h 146 1

Is there any way to avoid including files with definitions of forward declared types for methods that I will not use?

+4
source share
2 answers

, UT ostream, . . , gmock .
- PrintTo, , , PrintTo, , . gmock 1.7 C++14 GCC.

// A class to determine if definition of a type is available or is it forward declared only
template <class, class = void>
struct IsDefined : std::false_type
{};

template <class T>
struct IsDefined<T, std::enable_if_t<std::is_object<T>::value 
                                     && not std::is_pointer<T>::value 
                                     && (sizeof(T) > 0)>>
    : std::true_type
{};

// It has to be in global namespace
template <
    typename T,
    typename = std::enable_if_t<
        not IsDefined<T>::value and 
        not std::is_pointer<T>::value and 
        std::is_object<T>::value>>
std::ostream& operator<<(std::ostream& os, const T& ref)
{
    os << "Forward declared ref: " << static_cast<const void*>(&ref);
    // assert(false); // Could assert here as we do not want it to be executed
    return os;
}
+1

, PrintTo, gtest TypeWithoutFormatter, . , , .

namespace Foo { void PrintTo(const Bar& x, ::std::ostream* os) { *os << "Bar " << &x; }}
0

All Articles