一个库中的函数与另一个库中的模板匹配

Edw*_*ard 6 c++ templates c++14 spdlog

我正在开发一个使用两个不同库的C++项目:spdlog用于日志记录,mutils-serialization用于将对象序列化为字节(用于通过网络发送).两个库都正确使用命名空间,但是当我尝试编写同时使用它们的程序时,我的编译器(g ++ 6.2)给了我无意义的错误,这些错误似乎表明它试图从spdlog库中实例化一个函数模板通过使用mutils库中的函数模板的定义.

这是我的简单测试程序:

#include <spdlog/spdlog.h>
#include <spdlog/fmt/ostr.h>
#include "TestSerializableObject.h"

int main(int argc, char** argv) {
    auto global_logger = spdlog::rotating_logger_mt("global_logger", "log", 1024 * 1024 * 500, 3);
    global_logger->set_pattern("[%H:%M:%S.%e] [%l] %v");
    global_logger->set_level(spdlog::level::trace);

    std::shared_ptr<spdlog::logger> logger(spdlog::get("global_logger"));

    auto message = std::make_shared<messaging::TestSerializableObject>(
        1, 2, "A message!");

    logger->trace("Received a message: {}", *message);
}
Run Code Online (Sandbox Code Playgroud)

TestSerializableObject是一个实现的简单类mutils::ByteRepresentable(启用序列化和拉入mutils-serialization库的接口),并提供operator<<(spdlog能够记录它所需的).如有必要,我可以发布代码.

当我编译它时g++ -std=c++14 -I"./src" -I"./libraries" -I"./libraries/mutils/" -L"./libraries/" -O0 -g3 -Wall "src/LibraryCollisionTest.cpp",我得到了这个冗长,丑陋的错误(别担心,我会帮你解析它):

In file included from ./libraries/mutils/mutils.hpp:3:0,
                 from ./libraries/mutils-serialization/SerializationSupport.hpp:2,
                 from src/TestSerializableObject.h:10,
                 from src/LibraryCollisionTest.cpp:10:
./libraries/mutils/args-finder.hpp: In instantiation of ‘struct mutils::function_traits<messaging::TestSerializableObject>’:
./libraries/mutils/args-finder.hpp:75:41:   required from ‘auto mutils::convert(F) [with F = messaging::TestSerializableObject; ignore = void]’
./libraries/spdlog/fmt/bundled/format.h:1276:46:   required from ‘struct fmt::internal::ConvertToInt<messaging::TestSerializableObject>’
./libraries/spdlog/fmt/bundled/format.h:1485:5:   required by substitution of ‘template<class T> fmt::internal::MakeValue<Formatter>::MakeValue(const T&, typename fmt::internal::EnableIf<fmt::internal::Not<fmt::internal::ConvertToInt<T>::value>::value, int>::type) [with T = messaging::TestSerializableObject]’
./libraries/spdlog/fmt/bundled/format.h:2465:12:   required from ‘static fmt::internal::Value fmt::internal::ArgArray<N, true>::make(const T&) [with Formatter = fmt::BasicFormatter<char>; T = messaging::TestSerializableObject; unsigned int N = 1u]’
./libraries/spdlog/fmt/bundled/format.h:2898:5:   required from ‘void fmt::BasicWriter<Char>::write(fmt::BasicCStringRef<CharType>, const Args& ...) [with Args = {messaging::TestSerializableObject}; Char = char]’
./libraries/spdlog/details/logger_impl.h:69:9:   required from ‘void spdlog::logger::log(spdlog::level::level_enum, const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
./libraries/spdlog/details/logger_impl.h:127:5:   required from ‘void spdlog::logger::trace(const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
src/LibraryCollisionTest.cpp:21:53:   required from here
./libraries/mutils/args-finder.hpp:12:37: error: ‘operator()’ is not a member of ‘messaging::TestSerializableObject’
   : public function_traits<decltype(&T::operator())>
                                     ^~
./libraries/mutils/args-finder.hpp: In instantiation of ‘auto mutils::convert(F) [with F = messaging::TestSerializableObject; ignore = void]’:
./libraries/spdlog/fmt/bundled/format.h:1276:46:   required from ‘struct fmt::internal::ConvertToInt<messaging::TestSerializableObject>’
./libraries/spdlog/fmt/bundled/format.h:1485:5:   required by substitution of ‘template<class T> fmt::internal::MakeValue<Formatter>::MakeValue(const T&, typename fmt::internal::EnableIf<fmt::internal::Not<fmt::internal::ConvertToInt<T>::value>::value, int>::type) [with T = messaging::TestSerializableObject]’
./libraries/spdlog/fmt/bundled/format.h:2465:12:   required from ‘static fmt::internal::Value fmt::internal::ArgArray<N, true>::make(const T&) [with Formatter = fmt::BasicFormatter<char>; T = messaging::TestSerializableObject; unsigned int N = 1u]’
./libraries/spdlog/fmt/bundled/format.h:2898:5:   required from ‘void fmt::BasicWriter<Char>::write(fmt::BasicCStringRef<CharType>, const Args& ...) [with Args = {messaging::TestSerializableObject}; Char = char]’
./libraries/spdlog/details/logger_impl.h:69:9:   required from ‘void spdlog::logger::log(spdlog::level::level_enum, const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
./libraries/spdlog/details/logger_impl.h:127:5:   required from ‘void spdlog::logger::trace(const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
src/LibraryCollisionTest.cpp:21:53:   required from here
./libraries/mutils/args-finder.hpp:75:41: error: ‘as_function’ is not a member of ‘mutils::function_traits<messaging::TestSerializableObject>’
   return function_traits<F>::as_function(f);
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
In file included from ./libraries/spdlog/fmt/fmt.h:21:0,
                 from ./libraries/spdlog/common.h:41,
                 from ./libraries/spdlog/spdlog.h:12,
                 from src/LibraryCollisionTest.cpp:8:
./libraries/spdlog/fmt/bundled/format.h: In instantiation of ‘struct fmt::internal::ConvertToInt<messaging::TestSerializableObject>’:
./libraries/spdlog/fmt/bundled/format.h:1485:5:   required by substitution of ‘template<class T> fmt::internal::MakeValue<Formatter>::MakeValue(const T&, typename fmt::internal::EnableIf<fmt::internal::Not<fmt::internal::ConvertToInt<T>::value>::value, int>::type) [with T = messaging::TestSerializableObject]’
./libraries/spdlog/fmt/bundled/format.h:2465:12:   required from ‘static fmt::internal::Value fmt::internal::ArgArray<N, true>::make(const T&) [with Formatter = fmt::BasicFormatter<char>; T = messaging::TestSerializableObject; unsigned int N = 1u]’
./libraries/spdlog/fmt/bundled/format.h:2898:5:   required from ‘void fmt::BasicWriter<Char>::write(fmt::BasicCStringRef<CharType>, const Args& ...) [with Args = {messaging::TestSerializableObject}; Char = char]’
./libraries/spdlog/details/logger_impl.h:69:9:   required from ‘void spdlog::logger::log(spdlog::level::level_enum, const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
./libraries/spdlog/details/logger_impl.h:127:5:   required from ‘void spdlog::logger::trace(const char*, const Args& ...) [with Args = {messaging::TestSerializableObject}]’
src/LibraryCollisionTest.cpp:21:53:   required from here
./libraries/spdlog/fmt/bundled/format.h:1276:38: warning: invalid application of ‘sizeof’ to a void type [-Wpointer-arith]
     enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) };
Run Code Online (Sandbox Code Playgroud)

关键在这里:

./libraries/mutils/args-finder.hpp: In instantiation of ‘auto mutils::convert(F) 
 [with F = messaging::TestSerializableObject; ignore = void]’:
./libraries/spdlog/fmt/bundled/format.h:1276:46:   required from ‘struct
 fmt::internal::ConvertToInt<messaging::TestSerializableObject>’
./libraries/spdlog/fmt/bundled/format.h:1485:5:   required by substitution of 
 ‘template<class T> fmt::internal::MakeValue<Formatter>::MakeValue(const
 T&, typename fmt::internal::EnableIf<fmt::internal::Not<
 fmt::internal::ConvertToInt<T>::value>::value, int>::type) [with T =
 messaging::TestSerializableObject]’
Run Code Online (Sandbox Code Playgroud)

不知何故,g ++已经从spdlog库中的模板化函数(在命名空间中)扩展到mutils库中fmt::internal的函数模板,在命名空间中mutils,这显然不是spdlog库的目的!如果我查看第1276行format.h,那就是在这个模板结构中调用"convert"函数的那个​​:

template<typename T>
struct ConvertToInt
{
    enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) };
    enum { value = ConvertToIntImpl2<T, enable_conversion>::value };
};
Run Code Online (Sandbox Code Playgroud)

上面几行,当然,功能是"转换":

template <typename T>
T &get();

Yes &convert(fmt::ULongLong);
No &convert(...);
Run Code Online (Sandbox Code Playgroud)

这些都在命名空间内fmt::internal,我的IDE同意如果我想在1276行上定义函数"convert",我应该跳到第1248行的"convert"函数.那么为什么g ++会忽略这个定义,而是尝试使用定义mutils::convert(),即使在正确的命名空间中也是如此?

请注意,clang也无法编译该程序,并犯了同样的错误,所以我不认为这是g ++中的错误.

ild*_*arn 9

这绝对是spdlog中的一个错误.

问题在此FAQ草率描述:
什么是"论证依赖查找"(又名ADL,或"Koenig查找")?

因为messaging::TestSerializableObject从在命名空间的类型继承mutils,当convert被调用从内部命名空间不合格fmt::internalTestSerializableObject,两者fmt::internal::convert mutils::convert在过载组被考虑.变量函数在重载决策期间总是排在最后,因此F后者中的模板参数比前者更好...,并且mutils::convert被选中.

这绝不是特定于您的代码或mutils的 - 任何具有convert在同一命名空间或父命名空间中命名的一元函数或函数模板的类型都容易受到此问题的影响.

修复是有资格的convert通话和改变的定义fmt::internal::ConvertToInt<T>::enable_conversion

enum { enable_conversion = sizeof(convert(get<T>())) == sizeof(Yes) };
Run Code Online (Sandbox Code Playgroud)

enum { enable_conversion = sizeof(internal::convert(get<T>())) == sizeof(Yes) };
Run Code Online (Sandbox Code Playgroud)

在我自己的代码中,我习惯于始终限定对任何internal/ detail名称空间内的函数的所有调用,即使是来自同一名称空间内的代码,除非明确指出ADL用法.(Nb调用不需要完全合格,只需要合格.)我从观察中学到了这一教训,随着C++ 11的出现,Boost必须以艰难的方式处理这个问题.: - ]

  • 或者将助手移动到trait类中作为静态成员函数.如果名称解析为类成员,则没有ADL.人们也可以将`convert`括起来,尽管这可能太微妙了. (2认同)