clang support of aggregation initialization on Mac OS X - macos

I'm trying to compile C++11 list congregation initialization on clang++ on mac.
#include <iostream>
#include <list>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
list<string> aList = {"abc", "def", "xyz"};
}
This is the command for the compilation.
clang++-mp-3.1 -std=c++11 iterator.cpp
I got no matching constructor error.
iterator.cpp:7:23: error: no matching constructor for initialization of
'std::list<string>'
std::list<string> aList = {"abc", "def", "xyz"};
^ ~~~~~~~~~~~~~~~~~~~~~
I tried with XCode
clang -v
Apple clang version 4.1 (tags/Apple/clang-421.11.66) (based on LLVM 3.1svn)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
I also tried with clang++ from port
clang++-mp-3.1 -v
clang version 3.1 (branches/release_31)
Target: x86_64-apple-darwin11.4.2
Thread model: posix
I got the same result. What might be wrong?
clang's support of C++ 11 lambda

I tried icc, got the same error. I guess the issue is with the template not the compiler. icc uses existing template /usr/include/c++/4.2.1 and the implementation doesn't support c++11 fully.
I tried gcc 4.8 from port
sudo port install gcc48
It works fine
/opt/local/bin/g++-mp-4.8 -std=c++11 Iterator.cpp
How to turn on C++0x of Intel C++ Compiler 12.1.2

Related

C++11 code with thread giving error with clang version 3.5 on rpi3 but same works on my ubuntu linux machine

I am having a very small script as below :
#include <iostream>
#include <thread>
int main()
{
std::thread th([]() {
std::cout << "Hello, World!" << std::endl;
});
th.join();
return 0;
}
I am compiling it as below :
clang++ -o demo demo.cpp -lpthread -std=c++11
When i execute my binary after successful compilation, it gives below output :
on ubuntu linux machine :
Hello, World!
on my rpi3 :
pure virtual method called
terminate called without an active exception
Aborted
my clang version on both are :
on rpi3 :
Raspbian clang version 3.5.0-10+rpi1 (tags/RELEASE_350/final) (based on LLVM 3.5.0)
Target: arm-unknown-linux-gnueabihf
Thread model: posix
on linux :
clang version 3.8.1-24+rpi1 (tags/RELEASE_381/final)
Target: armv6--linux-gnueabihf
Thread model: posix
InstalledDir: /usr/bin
How to make this work ?
This is a bug in Clang. See bug report 23165. This appears even in clang 3.7 with -std=c++11.
As per the last comment on the bug report it seems to have been fixed in an update.
Verified fixed, with an update I just received via the llvm-toolchain-utopic channel to the clang-3.7 package. (package version: 1:3.7~svn234704-1~exp1)

No type named 'unique_ptr' in namespace 'std' when compiling under LLVM/Clang

I'm catching a compile error when attempting to use unique_ptr on Apple platforms with -std=c++11:
$ make
c++ -std=c++11 -DNDEBUG -g2 -O3 -fPIC -march=native -Wall -Wextra -pipe -c 3way.cpp
In file included ...
./smartptr.h:23:27: error: no type named 'unique_ptr' in namespace 'std'
using auto_ptr = std::unique_ptr<T>;
~~~~~^
./smartptr.h:23:37: error: expected ';' after alias declaration
using auto_ptr = std::unique_ptr<T>;
According to Marshall Clow, who I consider an expert on the C++ Standard Library with Clang and Apple:
Technical Report #1 (TR1) was a set of library additions to the C++03
standard. Representing the fact that they were not part of the
"official" standard, they were placed in the namespace std::tr1.
In c++11, they are officially part of the standard, and live in the
namespace std, just like vector and string. The include files no
longer live in the "tr1" folder, either.
Take aways:
Apple and C++03 = use TR1 namespace
Apple and C++11 = use STD namespace
Use LIBCPP_VERSION to detect libc++
Now, here's what I have in smartptr.h:
#include <memory>
// Manage auto_ptr warnings and deprecation in C++11
// Microsoft added template aliases to VS2015
#if (__cplusplus >= 201103L) || (_MSC_VER >= 1900)
template<typename T>
using auto_ptr = std::unique_ptr<T>;
#else
using std::auto_ptr;
#endif // C++11
I think the last thing to check is the __APPLE__ define, and here it is:
$ c++ -x c++ -dM -E - < /dev/null | grep -i apple
#define __APPLE_CC__ 6000
#define __APPLE__ 1
#define __VERSION__ "4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)"
#define __apple_build_version__ 5030040
Why am I receiving a error: no type named 'unique_ptr' in namespace 'std' when using -std=c++11?
I think these are the four test cases. It attempts to exercise the four configurations from the cross product of: {C++03,C++11} x {libc++,libstdc++}.
c++ -c test-clapple.cxx
OK
c++ -stdlib=libc++ -c test-clapple.cxx
OK
c++ -std=c++11 -c test-clapple.cxx
FAIL
c++ -std=c++11 -stdlib=libc++ -c test-clapple.cxx
OK
Here is the test driver. Be sure to test it on OS X so you get the full effects of the TR1 namespace in 2015.
$ cat test-clapple.cxx
// c++ -c test-clapple.cxx
// c++ -stdlib=libc++ -c test-clapple.cxx
// c++ -std=c++11 -c test-clapple.cxx
// c++ -std=c++11 -stdlib=libc++ -c test-clapple.cxx
#include <memory>
// Manage auto_ptr warnings and deprecation in C++11
#if (__cplusplus >= 201103L) || (_MSC_VER >= 1900)
template<typename T>
using auto_ptr = std::unique_ptr<T>;
#else
using std::auto_ptr;
#endif // C++11
int main(int argc, char* argv[])
{
return argc;
}
And the CFE Devs specifically told me to use that code;
No they didn't. They told you to do something similar if you want to use shared_ptr, because for C++03 <tr1/memory> defines std::tr1::shared_ptr and for C++11 <memory> defines std::shared_ptr.
But you're not using shared_ptr. If you want to use auto_ptr then it's just std::auto_ptr, everywhere, which is always defined in <memory>.
I think you've misunderstood Marshall's comment and you're overcomplicating things. What you quoted ('In c++11, they are officially part of the standard, and live in the namespace std, just like vector and string. The include files no longer live in the "tr1" folder, either.') is not Apple-specific or Clang-specific, it applies to all compilers. But since auto_ptr was never part of TR1 and never in <tr1/memory> it's irrelevant that the contents of TR1 are now in namespace std, because what you're trying to use was never included in TR1.
You should not be using TR1 at all here.
# include <memory>
// Manage auto_ptr warnings and deprecation in C++11
#if (__cplusplus >= 201103L)
template<typename T>
using auto_ptr = std::unique_ptr<T>;
#else
using std::auto_ptr;
#endif // C++11
This should be correct for modern compilers, but won't work on the stupid configuration that comes with XCode, which is a modern version of Clang that supports C++11 and the libstdc++ from GCC 4.2 which is nearly ten years old and doesn't support unique_ptr.
To cope with the default OS X toolchain this works:
#include <memory>
#if __cplusplus >= 201103L
# ifdef __clang__
# if __has_include(<forward_list>)
// either using libc++ or a libstdc++ that's new enough to have unique_ptr
# define HAVE_UNIQUE_PTR 1
# endif
# else // not clang, assume unique_ptr available
# define HAVE_UNIQUE_PTR 1
# endif
#endif
#ifdef HAVE_UNIQUE_PTR
template<typename T> using auto_ptr = std::unique_ptr<T>;
#else
using std::auto_ptr;
#endif
This works by using the presence of <forward_list> as an indicator of whether the standard library clang is using supports std::unique_ptr.
If clang is using libc++ as its standard library then all versions support unique_ptr and also provide <forward_list>, so the test passes.
If clang is using libstdc++ then whether unique_ptr is supported depends on the libstdc++ version. unique_ptr was added to libstdc++ in GCC 4.3, which is the same version that added <forward_list>, so if that header is available then unique_ptr will be too. If you are using clang with the ancient libstdc++ that ships with the Apple toolchains (from GCC 4.2) then unique_ptr is not supported, but neither is <forward_list>, so the test fails and you use auto_ptr instead.
That should work for any GCC/libstdc++, Clang/libc++ or Clang/libstdc++ combination found in the wild. I don't know what is needed for VC++/Dinkumware and Clang/Dinkumware, from your answer it looks like maybe you would just change the first condition to:
#if __cplusplus >= 201103L || _MSC_VER >= 1600

No type named 'nullptr_t' in namespace 'std'

Someone is compiling my Qt program that is using the C++11 standard and they got this error (Mac OS X / gcc). I know I can declare it, but shouldn't it be already in <cstddef>?
./collectable_smartptr.hpp:54:33: error: no type named 'nullptr_t' in namespace 'std'
void operator=(std::nullptr_t &null)
This code works just fine on Windows and Linux, I see it just on Mac.
Mac is i386-apple-darwin11.3.0, compiler is:
$ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
g++ options (some) are -c -pipe -std=c++11 -O2 -arch x86_64 -Xarch_x86_64 -mmacosx-version-min=10.5 -Wall -W -DQT_USE_QSTRINGBUILDER -DQT_NO_DEBUG -DQT_WEBKIT_LIB -DQT_XML_LIB -DQT_GUI_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED
Is this normal? Or is there something extra what needs to be done on Mac for C++11 to work?
It is always better to include things explicitly and so, I would add this to the code:
#include <cstddef>
If that does not work that means you have a fundamental issue with your system, namely:
You do not use a recent enough standard library implementation (e.g. try libc++).
You do not have the C++11 and on compilation enabled.
As a quick and nasty workaround, you could do the typedef yourself of course, as follows:
namespace std {
typedef decltype(nullptr) nullptr_t;
}
or without std, but this really ought to be the last resort, and usually it means you are doing something wrong.
Using #include <cstddef> didn't resolve the problem, but inserting
namespace std
{
typedef decltype(nullptr) nullptr_t;
}
did apparently help get through this problem, despite I still don't understand why it was really needed.
PS: Given that I am not owner of computer where this happens I can't do any further investigation, see https://github.com/huggle/huggle3-qt-lx/issues/101 for details
I had the same issue when I included sstream in the namespace std:
namespace std {
// Some #ifdefs and so on
// quite hidden
#include <sstream>
}
Moving the include out of the namespace resolved the issue:
#include <sstream>
namespace std {
}
Compiler: Apple LLVM version 8.0.0 (clang-800.0.38) without C++11.

c++ thread-local storage clang-503.0.40 (Mac OSX)

After I declared a variable in this way:
#include <thread>
namespace thread_space
{
thread_local int s;
} //etc.
i tried to compile my code using 'g++ -std=c++0x -pthread [sourcefile]'. I get the following error:
example.C:6:8: error: thread-local storage is unsupported for the current target
static thread_local int s;
^
1 error generated.
If i try to compile the same code on Linux with GCC 4.8.1 whit the same flags, i get a functioning executable file. I'm using clang-503.0.40 (the one which comes with Xcode 5.1.1) on a MacBook Pro running OSX 10.9.3. Can anybody explain me what i'm doing wrong?
Thank you!!
Try clang++ -stdlib=libc++ -std=c++11. OS X's outdated libstdc++ doesn't support TLS.
Edit
Ok, this works for the normal clang version but not for the Xcode one.
I did a diff against Apple's clang (503.0.38) and the normal released one and found the following difference:
.Case("cxx_thread_local",
- LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported() &&
- !PP.getTargetInfo().getTriple().isOSDarwin())
+ LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported())
So I think this is a bug in Apple's clang version (or they kept it in there on purpose - but still weird, because -v says based on 3.4).
Alternatively, you can use compiler extensions such as __thread (GCC/Clang) or __declspec(thread) (Visual Studio).
Wrap it in a macro and you can easily port your code across different compilers and language versions:
#if HAS_CXX11_THREAD_LOCAL
#define ATTRIBUTE_TLS thread_local
#elif defined (__GNUC__)
#define ATTRIBUTE_TLS __thread
#elif defined (_MSC_VER)
#define ATTRIBUTE_TLS __declspec(thread)
#else // !C++11 && !__GNUC__ && !_MSC_VER
#error "Define a thread local storage qualifier for your compiler/platform!"
#endif
...
ATTRIBUTE_TLS static unsigned int tls_int;
The clang compiler included in the Xcode 8 Beta and GM releases supports the C++11 thread_local keyword with both -std=c++11 and -std=c++14 (as well as the GCC variants).
Earlier versions of Xcode apparently supported C-style thread local storage using the keywords __thread or _Thread_local, according to the WWDC 2016 video "What's New in LLVM" (see the discussion beginning at 5:50).
Seems like you might need to set the minimum OS X version you target to 10.7 or higher.

MinGW 4.8.1 C++11 thread support

I downloaded the version of MinGW from the official website: http://sourceforge.net/projects/mingw/files/ and installed it on my Windows 7 machine.
Running g++ --version gives me g++.exe (GCC) 4.8.1 and I believe GCC 4.8.1 has support for C++11 features, including threads.
Running g++ -std=c++11 main.cpp successfully compiles the following program.
//main.cpp
#include <memory>
int main() {
std::unique_ptr<int> a(new int);
return 0;
}
But running g++ -std=c++11 main.cpp on the following program:
//main.cpp
#include <mutex>
int main() {
std::mutex myMutex;
return 0;
}
gives errors:
main.cpp: In function `int main()`:
main.cpp:5:5: error: 'mutex' is not a member of 'std'
std::mutex myMutex;
^
main.cpp:5:16: error: expected ';' before 'myMutex'
std::mutex myMutex;
^
as if <mutex> is not supported. The compiler does not complain about #include <mutex> so I have no idea why I'm getting this error.
If I understand well, std threading is still not supported on mingw, but some mingw-w64 builds support it. Fortunately, you can still build 32-bit apps using this version of mingw.
Here is the link for the builds.
There is already a native win32 implementation of std::thread and sync primitives:
https://github.com/meganz/mingw-std-threads
It is a header-only library and should work with any C++11 compliant version of MinGW.

Resources