为什么使用shared_ptr的atomic_load不能用gcc 4.9编译?

时间:2015-12-10 15:35:38

标签: c++11 gcc

这不使用gcc 4.9.3(来自docker repo)进行编译:

#include <memory>
#include <atomic>

int main() {
    auto p = std::make_shared<int>(3);
    auto n = std::atomic_load(&p);
}

使用gcc docker版本进行测试(您只需复制粘贴整个块):

docker run -it gcc:4.9 bash
# create a test.cpp with previous code:
cat > test.cpp << EOF
#include <memory>
#include <atomic>

int main() {
 auto p = std::make_shared<int>(3);
 auto n = std::atomic_load(&p);
}

EOF
# and compile:
g++ -std=c++11 test.cpp -o bla

无法使用4.9进行编译,使用5.1成功,尽管the C++0x/C++11 compatibility page并未提及任何问题

只是在这里张贴这个,因为我只是花了几个小时来弄清问题,所以没有其他人不得不浪费时间:)

编辑:添加编译器输出:

test.cpp: In function 'int main()':
test.cpp:6:30: error: no matching function for call to 'atomic_load(std::shared_ptr<int>*)'
  auto n = std::atomic_load(&p);
                              ^
test.cpp:6:30: note: candidates are:
In file included from test.cpp:2:0:
/usr/local/include/c++/4.9.3/atomic:906:5: note: template<class _ITp> _ITp std::atomic_load(const std::atomic<_ITp>*)
     atomic_load(const atomic<_ITp>* __a) noexcept
     ^
/usr/local/include/c++/4.9.3/atomic:906:5: note:   template argument deduction/substitution failed:
test.cpp:6:30: note:   'std::shared_ptr<int>' is not derived from 'const std::atomic<_ITp>'
  auto n = std::atomic_load(&p);
                              ^
In file included from test.cpp:2:0:
/usr/local/include/c++/4.9.3/atomic:911:5: note: template<class _ITp> _ITp std::atomic_load(const volatile std::atomic<_ITp>*)
     atomic_load(const volatile atomic<_ITp>* __a) noexcept
     ^
/usr/local/include/c++/4.9.3/atomic:911:5: note:   template argument deduction/substitution failed:
test.cpp:6:30: note:   'std::shared_ptr<int>' is not derived from 'const volatile std::atomic<_ITp>'
  auto n = std::atomic_load(&p);
                              ^

1 个答案:

答案 0 :(得分:2)

这是libstdc ++库中的遗漏。见bug report。 “运行时库”部分的GCC 5 Release notes中提到了此功能的添加。 它没有显示在您的链接上,因为它是库,而不是编译器功能。 图书馆功能支持page,但遗憾的是它没有说明功能首次出现在哪个版本中。