gcc 10 support for atomic shared_ptr - c++

I thought g++ 10.3 should have supported the C++20 feature of atomic shared_ptr? But I am still getting the following error
#include <atomic>
#include <thread>
#include <memory>
int main() {
std::atomic<std::shared_ptr<int>> a = std::make_shared<int>(1);
}
In file included from test.cc:1:
/usr/include/c++/10/atomic: In instantiation of ‘struct std::atomic<std::shared_ptr<int> >’:
test.cc:6:37: required from here
/usr/include/c++/10/atomic:195:21: error: static assertion failed: std::atomic requires a trivially copyable type
195 | static_assert(__is_trivially_copyable(_Tp),
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

The documentation lists it as supported only since GCC 12.1, i.e. the next release, and compiler explorer shows that your code compiles on GCC trunk: https://godbolt.org/z/4ThzMrjM9
https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

Related

How to fix lib libstdc++'s `std::variant` (from GCC 9.1) not working with Clang 8?

MSYS2's GCC package was recently updated to 9.1, but Clang doesn't like the new <variant> libstdc++ header that comes with it.
When compiling following simple program:
#include <variant>
int main()
{
std::variant<int, float> x;
}
I get:
# clang++ -std=c++17 foo.cpp
In file included from foo.cpp:1:
Z:\...\msys2\mingw64\include\c++\9.1.0\variant:1559:55: error: '__get' is missing exception specification 'noexcept'
friend constexpr decltype(auto) __detail::__variant::__get(_Vp&& __v);
^
foo.cpp:5:30: note: in instantiation of template class 'std::variant<int, float>' requested here
std::variant<int, float> x;
^
Z:\...\msys2\mingw64\include\c++\9.1.0\variant:263:5: note: previous declaration is here
__get(_Variant&& __v) noexcept
^
1 error generated.
Here is the complete <variant> header if you want to look at it.
While I'm waiting for an official fix, I did as Clang suggested and added noexcept to the header.
It seems to work so far.
Can this solution cause any problems? Should I do something else?
Bonus points if you know if it's a libstdc++ bug or a Clang bug.
The fix is correct. It's a libstdc++ bug, see https://bugs.llvm.org/show_bug.cgi?id=41863 and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90397

atomic operations on shared pointers, c++ versions

I was working on this thread swap c++ map objects in multithreaded environment
However,
#include <memory>
#include <thread>
#include <chrono>
#include <atomic>
#include <iostream>
using namespace std;
shared_ptr<std::string> the_string;
int main()
{
atomic_store(&the_string, std::make_shared<std::string>("first string"));
}
gives a compile time error
error: no matching function for call to 'atomic_store'
atomic_store(&the_string, std::make_shared<std::string>("first string"));
^~~~~~~~~~~~
/Library/Developer/CommandLineTools/usr/include/c++/v1/atomic:1165:1: note: candidate template ignored: could not match 'atomic' against 'shared_ptr'
atomic_store(volatile atomic<_Tp>* __o, _Tp __d) _NOEXCEPT
I did see a few threads on this problem and understand that it could be related to C++ version I have /usr/include/c++/4.2.1/ and /usr/include/c++/4.8.5/ on another box, both give the same issue. Should I upgrade the C++ version?
I resolved this issue by passing -std=c++11 flag.
I resolved this issue by passing the flag -std=c++11
It compiles fine here with GCC 8.3 and Clang 8.0, so yes, you should upgrade your compiler.

Error with Intel C++ compiler: identifier "_Compare" is undefined

I am able to compile the following a std=c++11 required code with g++ using the following command:
g++ test.cpp -std=c++11 -Wl,-rpath,/share/apps/gcc/6.3.0/lib64
the code:
#include <chrono>
#include <map>
#include <memory>
#include <thread>
#include <utility>
int main() {
typedef std::unique_ptr<int> intPointer;
intPointer p(new int(10));
std::map<int, std::unique_ptr<int>> m;
m.insert(std::make_pair(5, std::move(p)));
auto start = std::chrono::system_clock::now();
if (std::chrono::system_clock::now() - start < std::chrono::seconds(2))
{
std::thread t;
}
}
The very same command(probably I don't know the correct one) wont work for intel compiler:
icpc test.cpp -std=c++11 -Wl,-rpath,/share/apps/intel/2016.1.056/vtune_amplifier_xe_2016.1.1.434111/target/linux64/lib64
The error is:
In file included from /share/apps/gcc/6.3.0/include/c++/6.3.0/map(60),
from test.cpp(2):
/share/apps/gcc/6.3.0/include/c++/6.3.0/bits/stl_tree.h(1437):
error: identifier "_Compare" is undefined
&& is_nothrow_move_assignable<_Compare>::value)
^
In file included from /share/apps/gcc/6.3.0/include/c++/6.3.0/map(60),
from test.cpp(2):
/share/apps/gcc/6.3.0/include/c++/6.3.0/bits/stl_tree.h(1778):
error: identifier "_Compare" is undefined
_GLIBCXX_NOEXCEPT_IF(__is_nothrow_swappable<_Compare>::value)
^
What am I doing wrong, and how should I fix this.
From the comments above:
_Compare is one of the template parameters of the _Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc> defined in that file, so surely it is defined.
A more likely reason is that the Intel compiler perhaps doesn't know about is_nothrow_move_assignable which is kind of recently added to the type_traits.
I had a user with a similar c++ difficulty using gcc-6.3.0 headers and the Intel 16.1.150 compiler. I opted for the compiler bug theory, tried with the Intel 17 compiler and things worked.
-doug
Well I checked with Intel agents and it turned out that Intel 16 do not support gcc6.3, the support comes in Intel 17.

clang++ error on <experimental/any>

I get an error when compile code containing <experimental/any>.
Code in main.cpp:
#include <experimental/any>
int main() { }
Compile this (clang version is 3.9):
clang++ main.cpp -o main -std=c++1z
Error after the compiling:
In file included from main.cpp:2:
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:364:34: error:
no template named '__any_caster'; did you mean 'any_cast'?
return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:361:30: note:
'any_cast' declared here
inline const _ValueType* any_cast(const any* __any) noexcept
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:372:34: error:
no template named '__any_caster'; did you mean 'any_cast'?
return static_cast<_ValueType*>(__any_caster<_ValueType>(__any));
^
/usr/lib/gcc/x86_64-linux-gnu/5.4.0/../../../../include/c++/5.4.0/experimental/any:369:24: note:
'any_cast' declared here
inline _ValueType* any_cast(any* __any) noexcept
^
2 errors generated.
As #chris mentioned in the comments:
You could try with libc++. Perhaps there's an incompatibility with Clang in libstdc++'s new header.
This turns out to be true. Clang 3.9 is still experimental, and so it uses experimental headers, including a experimental C++ standard library. By default, it is provided by GCC, and so an incompatibility occurs between the GCC implementation and the Clang implementation.

Why does this basic thread program fail with Clang but pass in g++?

I have this simple program that works with threads. In Clang I get a bunch of confusing irrelevant errors. Here is the program:
#include <iostream>
#include <thread>
#include <future>
int main()
{
std::packaged_task<int()> task([] { return 1; });
std::future<int> result = task.get_future();
task();
std::cout << "Result was: " << result.get();
}
Errors:
error: no matching constructor for initialization of 'duration' (aka 'std::chrono::duration<long, std::ratio<1, 1000000> >')
: _d(_t.time_since_epoch())
note: in instantiation of function template specialization 'std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<long, std::ratio<1, 1000000> > >::time_point<std::chrono::duration<long, std::ratio<1, 1000000000> > >' requested here
There's a lot more, but you can see it in this link of the program. Oddly, it compiles fine in g++ 4.7.3 and 4.6.3. Why is this only happening in Clang?
Update: As David pointed out, it seems only to be failing when I include the <future> header.
This is a documented bug in clang/libstdc++.
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=666539
http://llvm.org/bugs/show_bug.cgi?id=12893
From Clang's status page:
Clang's C++11 mode can be used with libc++ or with gcc's libstdc++, but patches are needed to make libstdc++-4.4 work with Clang in C++11 mode. Patches are also needed to make libstdc++-4.6, and libstdc++-4.7 work with Clang releases prior to version 3.2 in C++11 mode.
This is an incompatibility between libstdc++ and clang. If you were to build against libstdc++ 4.8.0, this problem goes away.
[11:43am][wlynch#apple /tmp] /opt/llvm/3.2/bin/clang++ -std=gnu++11 -gcc-toolchain /opt/gcc/4.8.0 se.cc |& wc -l
0
[11:43am][wlynch#apple /tmp] /opt/llvm/3.2/bin/clang++ -std=gnu++11 -gcc-toolchain /opt/gcc/4.7.2 se.cc |& wc -l
21
If this is happening with clang 3.2, it's not bug 12893 (which was fixed in clang 3.2).
It is more likely to be this bug, which is really a bug in libstdc++ 4.7: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53841