微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

除非我包含“grpcpp/grpcpp.h”

如何解决除非我包含“grpcpp/grpcpp.h”

我偶然发现了一个有点奇怪的问题

我的mongodb单例从here.看到的版本略有修改

// mongodb_access.h
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/logger.hpp>
#include <mongocxx/pipeline.hpp>
#include <mongocxx/pool.hpp>
#include <mongocxx/uri.hpp>

#include <bsoncxx/stdx/make_unique.hpp>
#include <bsoncxx/stdx/optional.hpp>
#include <bsoncxx/stdx/string_view.hpp>

class mongodb_access {
public:
  static mongodb_access &instance() {
    static mongodb_access instance;
    return instance;
  }

  void configure(std::unique_ptr<mongocxx::instance> instance,std::unique_ptr<mongocxx::pool> pool) {
    _instance = std::move(instance);
    _pool = std::move(pool);
  }

  using connection = mongocxx::pool::entry;

  connection get_connection() { return _pool->acquire(); }

  bsoncxx::stdx::optional<connection> try_get_connection() {
    return _pool->try_acquire();
  }

private:
  mongodb_access() = default;

  std::unique_ptr<mongocxx::instance> _instance = nullptr;
  std::unique_ptr<mongocxx::pool> _pool = nullptr;
};

inline void configure(mongocxx::uri uri) {
  class noop_logger : public mongocxx::logger {
  public:
    virtual void operator()(mongocxx::log_level,bsoncxx::stdx::string_view,bsoncxx::stdx::string_view) noexcept {}
  };

  auto instance = bsoncxx::stdx::make_unique<mongocxx::instance>(
      bsoncxx::stdx::make_unique<noop_logger>());

  mongodb_access::instance().configure(
      std::move(instance),bsoncxx::stdx::make_unique<mongocxx::pool>(std::move(uri)));
}

我从我的一个翻译单元(一个首先创建的)配置 uri,然后我使用 auto conn = mongodb_access::instance().get_connection(); 在我需要的地方获取连接。

例如:

// foo.cpp

#include "grpcpp/grpcpp.h" // <- This is needed for some reason
#include "mongodb_access.h"

#include "bsoncxx/builder/basic/array.hpp"
#include "bsoncxx/builder/basic/document.hpp"
#include "bsoncxx/builder/basic/kvp.hpp"
#include "bsoncxx/json.hpp"
#include "bsoncxx/types.hpp"
#include "mongocxx/exception/exception.hpp"

class foo
{

foo()
{
    // configure db connections
  auto uri = mongocxx::uri{mongocxx::uri::k_default_uri};
  configure(std::move(uri));
}

void some_random_function()
{
    auto conn = mongodb_access::instance().get_connection();
    /// ...
}

}

除非我从 grpcpp/grpcpp.h 添加 grpc 作为第一次导入(或至少在包含 mongodb_access.h 之前),否则这不会编译

如果我不这样做,我会收到一堆错误

[build] D:\Libs\vcpkg\installed\x64-windows\include\mongocxx/options/find.hpp(216,11): warning C4003: not enough arguments for function-like macro invocation 'max' 
[build] D:\Libs\vcpkg\installed\x64-windows\include\mongocxx/options/find.hpp(216,11): error C2146: Syntax error: missing ')' before identifier 'max' 
[build] D:\Libs\vcpkg\installed\x64-windows\include\mongocxx/options/find.hpp(216,11): error C3646: 'max': unkNown override specifier 
[build] D:\Libs\vcpkg\installed\x64-windows\include\mongocxx/options/find.hpp(216,11): error C2059: Syntax error: ')' 
[build] D:\Libs\vcpkg\installed\x64-windows\include\mongocxx/options/find.hpp(216,11): error C2098: unexpected token after data member 'view_or_value' 
[build] D:\Libs\vcpkg\installed\x64-windows\include\mongocxx/options/find.hpp(216,11): error C2059: Syntax error: '>' 
[build] D:\Libs\vcpkg\installed\x64-windows\include\mongocxx/options/find.hpp(216,11): error C2059: Syntax error: '?' 
[build] D:\Libs\vcpkg\installed\x64-windows\include\mongocxx/options/find.hpp(216,11): error C2334: unexpected token(s) preceding ':'; skipping apparent function body 

... and many others

任何帮助将不胜感激。

解决方法

这看起来像是包含污染的典型副作用,并且很可能与 grpc 本身无关。 grpc 包含的标题之一正在解决您的问题。您可以尝试使用 gcc -E 缩小范围以显示预处理代码。我的直觉告诉我你失踪了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。