gpt4 book ai didi

c++ - 将大括号括起来的初始值设定项列表转换为类型时出错的含义?

转载 作者:太空狗 更新时间:2023-10-29 21:48:58 25 4
gpt4 key购买 nike

我正在尝试移植/构建 g++ 以在我的系统上运行,并在构建 libstdc++ 时遇到以下错误:

.../gcc-4.6.2/i686-pc-linux-gnu/libstdc++-v3/include/mutex:226:50: error: could not convert '{0}' from '<brace-enclosed initializer list>' to 'std::timed_mutex::__native_type {aka pthread_mutex_t}'

include/mutex中的相关代码是:

class timed_mutex
{
// ...
__native_type _M_mutex;
// ...
timed_mutex() : _M_mutex(__GTHREAD_MUTEX_INIT) { } // Line 226
// ...
}

__native_typepthread_mutex_t__GTHREAD_MUTEX_INIT扩展为 {0} .

我对 C++ 一点也不熟悉,只熟悉 C,但我看不出这里有什么明显的错误。错误是什么意思?

最佳答案

正确的语法是:

timed_mutex() : _M_mutex({__GTHREAD_MUTEX_INIT}) { }  // i.e. _M_mutex({{0}})

但是,该功能仅适用于 C++11Demo .
对于较旧的编译器,您不能将初始化列表与构造函数一起使用。

有 2 个的原因 {}是,pthread_mutex_tunion定义为 shown here .其中包含 struct , char[24] , long int ;因此初始化语法自然会有所不同。

更新:当我试图编译 <mutex>测试文件中的 header ,它给出以下错误:

/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

很可能特定文件遵循 C++11 的初始化程序语法

关于c++ - 将大括号括起来的初始值设定项列表转换为类型时出错的含义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9510194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com