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.
Related
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
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
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.
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
MinGw 4.7.0 Boost 1.47
code:
#include <iostream>
#include <boost/thread.hpp>
int main(int argc, char *argv[])
{
std::cout<<"In main"<<std::endl;
}
Compiler string:
g++.exe -Wall -fexceptions -g -IC:\soft\ides_comp\mingw\include\boost_1_47_0 -c D:\work\cpp_cb\mt1\main.cpp -o obj\Debug\main.o
It doesnt compile and i have same error whatever compiler options i tried.
Warning:
In file included from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_data.hpp:12:0,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread.hpp:15,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread.hpp:13,
from D:\work\cpp_cb\mt1\main.cpp:2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:59:40: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int)' declared as dllimport: attribute ignored [-Wattributes]
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:69:39: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared as dllimport: attribute ignored [-Wattributes]
Error:
In file included from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/shared_ptr.hpp:30:0,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/shared_ptr.hpp:17,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/date_time/time_clock.hpp:17,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread_time.hpp:9,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/win32/thread_data.hpp:10,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread/thread.hpp:15,
from C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/thread.hpp:13,
from D:\work\cpp_cb\mt1\main.cpp:2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/checked_delete.hpp: In instantiation of 'void boost::checked_delete(T*) [with T = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]':
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/detail/shared_count.hpp:95:13: required from 'boost::detail::shared_count::shared_count(Y*) [with Y = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/smart_ptr/shared_ptr.hpp:183:50: required from 'boost::shared_ptr<T>::shared_ptr(Y*) [with Y = boost::error_info<boost::tag_original_exception_type, const std::type_info*>; T = boost::error_info<boost::tag_original_exception_type, const std::type_info*>]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/info.hpp:171:69: required from 'const E& boost::exception_detail::set_info(const E&, const boost::error_info<Tag, T>&) [with E = boost::unknown_exception; Tag = boost::tag_original_exception_type; T = const std::type_info*]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/info.hpp:192:46: required from 'typename boost::enable_if<boost::exception_detail::derives_boost_exception<E>, const E&>::type boost::operator<<(const E&, const boost::error_info<Tag, T>&) [with E = boost::unknown_exception; Tag = boost::tag_original_exception_type; T = const std::type_info*; typename boost::enable_if<boost::exception_detail::derives_boost_exception<E>, const E&>::type = const boost::unknown_exception&]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/detail/exception_ptr.hpp:182:13: required from 'void boost::unknown_exception::add_original_type(const E&) [with E = std::exception]'
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/exception/detail/exception_ptr.hpp:161:32: required from here
Warning 2:
C:\soft\ides_comp\mingw\include\boost_1_47_0/boost/checked_delete.hpp:34:5: warning: deleting object of polymorphic class type 'boost::error_info<boost::tag_original_exception_type, const std::type_info*>' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]
What can it be? Thanks
This has been fixed in later boost versions. Check here:
https://svn.boost.org/trac/boost/ticket/6165
Update boost/config/stdlib/libstdcpp3.hpp:
#if defined(_GLIBCXX_HAVE_GTHR_DEFAULT) \
|| defined(_GLIBCXX_HAS_GTHREADS) \
|| defined(_GLIBCXX__PTHREADS) \
|| defined(WIN32)
Added _GLIBCXX_HAS_GTHREADS and/or WIN32 (from the link above, see patch).
WIN32 seems a bit brute force, but doesn't work without (at least not for me. I am using an older boost version 1.42).
If you compiled the thread library for using Win32 threads, you might need
-DBOOST_USE_WINDOWS_H
in your compiler flags. While I don't think that's the issue (I get a different set of errors for not using it), you might give it a try.
This answer maybe not very straight, but:
I do use g++ 4.7.0 20111209 and it has support for threads.
#include <iostream>
#include <thread>
using namespace std;
void foo()
{
cout << "Hello from thread\n";
}
int main()
{
thread th1(foo);
th1.join();
return 0;
}
During compilation use -std=c++11 option to use the latest standard which supports threads
PS:
boost 1.48 compiled on my pc with mingw 4.7.0 gives an error (when trying to use boost::thread):
Threading support unavailable: it has been explicitly disabled with BOOST_DISABLE_THREADS
So I guess it was disabled by default with g++ 4.7.0 (with g++ 4.6.2 it was not).