googletest icon indicating copy to clipboard operation
googletest copied to clipboard

[Bug]: Conflict with nlohmann json library

Open sergio-nsk opened this issue 2 years ago • 0 comments

Describe the issue

The smallest code does not compile (https://godbolt.org/z/znYd68E13)

#include "gtest/gtest.h"
#include "nlohmann/json.hpp"

void operator<<(nlohmann::json&, int);  // The second parameter can be of any custom type T.

int main() {
  nlohmann::json json;
  json << 1;  // Or any custom type T{}
}

If #include "gtest/gtest.h" is removed, the code compiles.

The error is long, please see the compiler explorer. It starts with

include/c++/12.2.0/type_traits:971:30: error: incomplete type 'testing::internal::Secret' used in type trait expression
<...>
<source>:8:3: note: while substituting deduced template arguments into function template 'operator type-parameter-0-0' [with ValueType = testing::internal::Secret, $1 = (no value)]
  json << 1;
  ^

I don't get how the compiler deduces ValueType = testing::internal::Secret here:

# 21096 "/opt/compiler-explorer/libs/nlohmann_json/trunk/single_include/nlohmann/json.hpp" 3
    template < typename ValueType, typename std::enable_if <
                   detail::conjunction <
                       detail::negation<std::is_pointer<ValueType>>,
                       detail::negation<std::is_same<ValueType, std::nullptr_t>>,
                       detail::negation<std::is_same<ValueType, detail::json_ref<basic_json>>>,
                                        detail::negation<std::is_same<ValueType, typename string_t::value_type>>,
                                        detail::negation<detail::is_basic_json<ValueType>>,
                                        detail::negation<std::is_same<ValueType, std::initializer_list<typename string_t::value_type>>>,

                                                detail::negation<std::is_same<ValueType, std::string_view>>,

                                                detail::negation<std::is_same<ValueType, std::any>>,

                                                detail::is_detected_lazy<detail::get_template_function, const basic_json_t&, ValueType>
                                                >::value, int >::type = 0 >
                                                      operator ValueType() const
    {

        return get<ValueType>();
    }

I guess it might be related to void operator<<(const testing::internal::Secret&, int);

Steps to reproduce the problem

Try to compile the small code above.

What version of GoogleTest are you using?

Main branches (trunk) of Google Test and nlohmann::json.

What operating system and version are you using?

Compiler explorer containers are either Ubuntu or Alpine Linux.

What compiler and version are you using?

Clang 16.0.0 w/ and w/o -stdlib=libc++ GCC 13.1

What build system are you using?

Just command line.

Additional context

I am trying to fix the issue. Unfortunately it's a late night time already.

sergio-nsk avatar Jun 13 '23 05:06 sergio-nsk