首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >'google::protobuf::internal::CallOnInitializedMutex协议缓冲区:非文字类型的临时C++ <std::mutex>‘在常量表达式中

'google::protobuf::internal::CallOnInitializedMutex协议缓冲区:非文字类型的临时C++ <std::mutex>‘在常量表达式中
EN

Stack Overflow用户
提问于 2021-09-18 06:45:50
回答 2查看 1.5K关注 0票数 1

我正在使用来自这里的Protoc-3.18.0-Win 32版本。成功编译.proto文件后,QtCreator 5 (C++11)程序中出现以下错误:

代码语言:javascript
运行
复制
C:\Users\MyName\MyProject\lib\include\google\protobuf\stubs\mutex.h:124: error: temporary of non-literal type 'google::protobuf::internal::CallOnceInitializedMutex<std::mutex>' in a constant expression
In file included from lib\include/google/protobuf/descriptor.h:66:0,
                 from lib\include/google/protobuf/message.h:121,
                 from lib\include/google/protobuf/generated_message_bases.h:42,
                 from src/protodata/myfile.pb.h:26,
                 from src/myfile/myfile.h:12,
                 from src\myclass/myclass.h:8,
                 from src\mywidget.cpp:2:
lib\include/google/protobuf/stubs/mutex.h: In constructor 'constexpr google::protobuf::internal::WrappedMutex::WrappedMutex()':
lib\include/google/protobuf/stubs/mutex.h:124:29: error: temporary of non-literal type 'google::protobuf::internal::CallOnceInitializedMutex<std::mutex>' in a constant expression
   constexpr WrappedMutex() {}
                             ^
lib\include/google/protobuf/stubs/mutex.h:98:7: note: 'google::protobuf::internal::CallOnceInitializedMutex<std::mutex>' is not literal because:
 class CallOnceInitializedMutex {
       ^~~~~~~~~~~~~~~~~~~~~~~~
lib\include/google/protobuf/stubs/mutex.h:98:7: note:   'google::protobuf::internal::CallOnceInitializedMutex<std::mutex>' has a non-trivial destructor

错误的代码行为:

代码语言:javascript
运行
复制
// Mutex is a natural type to wrap. As both google and other organization have
// specialized mutexes. gRPC also provides an injection mechanism for custom
// mutexes.
class GOOGLE_PROTOBUF_CAPABILITY("mutex") PROTOBUF_EXPORT WrappedMutex {
 public:
#if defined(__QNX__)
  constexpr WrappedMutex() = default;
#else
  constexpr WrappedMutex() {} // <--- Error points here
#endif
EN

回答 2

Stack Overflow用户

发布于 2021-12-22 19:53:53

在gcc 7.3和c++17中,我尝试使用高于3.15.0的protobuf版本时也遇到了同样的问题。

在查看了protobuf生成的代码之后,我发现在3.15之后的版本中,protobuf生成的代码容器普遍存在着触发gcc错误的“constexpr”。

可能的解决办法:

  1. 使用更高版本的gcc,错误修正在7.4 (首选)
  2. 使用'-std=c++14‘代替'-std=c++17',这对我很有用。
  3. 使用早于3.15、3.13和3.14的protobuf为我工作。
票数 2
EN

Stack Overflow用户

发布于 2022-06-16 14:19:03

在我的例子中,这就足够了:转到opencv文件夹,找到文件\opencv\sources\3rdparty\protobuf\src\google\protobuf\stubs\mutex.h,在WrappedMutex类中删除第一个后面的行,

代码语言:javascript
运行
复制
class GOOGLE_PROTOBUF_CAPABILITY("mutex") PROTOBUF_EXPORT WrappedMutex {
 audience:
#if defined(__QNX__)
  constexpr WrappedMutex() = default;
#else
  constexpr WrappedMutex() {} // **Delete this line**
#endif
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69232278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档