No type named 'unique_ptr' in namespace 'std' when compiling under LLVM/Clang - c++11

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

Related

Undocumented ABI changes of std::function between GCC-4 and GCC-5/6/7/8/9, how to make a .so working with devtoolset-4/6/7/8/9?

with _GLIBCXX_USE_CXX11_ABI=0
std::function of GCC-4 is different of GCC-5 and follwing versions.
The following code show you the fact:
==> lib.cc <==
#include <functional>
std::function<int(const void*p)> holder;
int run_holder(const void *p)
{
return holder(p);
}
==> main.cc <==
#include <stdio.h>
#include <functional>
extern int run_holder(const void*p);
extern std::function<int(const void*p)> holder;
int foo(const void* p)
{
printf("p=%p\n", p);
return 0;
}
int main()
{
holder = foo;
foo((void*)0x12345678);
holder((void*)0x12345678);
run_holder((void*)0x12345678);
}
==> make.sh <==
#!/bin/bash
GCC4=/usr/bin/g++
GCCN="scl enable devtoolset-5 -- g++"
$GCC4 -std=c++11 -c -g lib.cc -shared -o libfoo.so &&
$GCCN -std=c++11 -L. -lfoo -g main.cc -o a.out &&
LD_LIBRARY_PATH=. ./a.out
expected result, something like:
p=0x12345678
p=0x12345678
p=0x12345678
actual result:
p=0x12345678
./make.sh: line 6: 973 Segmentation fault LD_LIBRARY_PATH=. ./a.out
The reason is the implementation of std::function changes without document.
gcc4: /usr/include/c++/4.8.2/functional:2430
typedef _Res (*_Invoker_type)(const _Any_data&, _ArgTypes...);
gcc5: /opt/rh/devtoolset-4/root/usr/include/c++/5.3.1/functional:2226
gcc8: /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/std_function.h:609
using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...);
So I can not write a .so using std::function compiled by gcc4 and used by gcc5/6/7/8. There is no macro like _GLIBCXX_USE_CXX11_ABI can control the behavior.
So I can not write a .so using std::function compiled by gcc4 and used by gcc5/6/7/8.
Correct. Neither Red Hat nor the GCC project has ever claimed that's possible, quite the opposite. C++11 support in GCC 4.x was incomplete and unstable, and subject to ABI changes and API changes. What you're trying to do was never supported.
I've explained this in more detail at https://stackoverflow.com/a/49119902/981959
The Developer Toolset documentation covers it too (emphasis mine):
"A compiler in C++11 or C++14 mode is only guaranteed to be compatible with another compiler in C++11 or C++14 mode if they are from the same release series (for example from Red Hat Developer Toolset 6.x).
...
"Using the C++14 language version is supported in Red Hat Developer Toolset when all C++ objects compiled with the respective flag have been built using Red Hat Developer Toolset 6 or later. Objects compiled by the system GCC in its default mode of C++98 are also compatible, but objects compiled with the system GCC in C++11 or C++14 mode are not compatible."
There is no macro like _GLIBCXX_USE_CXX11_ABI can control the behavior.
We do not provide macros to control things that are unsupported and cannot work.
If you want to use C++11 with a mix of GCC versions you need to use a release that has stable, non-experimental support for C++11. So not GCC 4.x.

gcc header error: '_mm256_set_m128d' was not declared in this scope

I'm trying to compile a large code that was compiled with icpc and mkl math libraries using gcc instead.
Here is the code that needs AVX.
#include <immintrin.h>
#include <stdint.h>
#include <math.h>
.
.
_mm256_set_m128d( _mm256_extractf128_pd(t2, 0) + _mm256_extractf128_pd(t2, 1),
_mm256_extractf128_pd(t1, 0) + _mm256_extractf128_pd(t1, 1));
I have -mavx flag enabled when compiling, as suggested on some posts to enable advanced vectorization.
_mm256_set_m128d is not defined in immintrin.h or any other in gcc include files (version 5.3).
I found that its available for intel headers though - in immintrin.h header provided with intel compiler.
Any suggestions how to fix this issue?
Thanks!
You can just define your own macro like this:
#define _mm256_set_m128d(vh, vl) \
_mm256_insertf128_pd(_mm256_castpd128_pd256(vl), (vh), 1)
Bracket it inside a suitable #ifdef/#endif so that it's only defined for gcc of course.

Test for C++11 in GCC 4.6

This fails to compile under GCC 4.6:
#if __cplusplus >= 201103L
#include <ratio>
#endif
__cplusplus should be set to "1" in GCC 4.6 according to this bug:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=1773
How should I test for C++11 support in GCC 4.6?
EDIT: I want to use Boost ratio when built with GCC 4.6 and std ratio with GCC 4.8 and -std=c++11. The rest of the code is ready for this, it's just this include that fails to compile.
Since even compilers which theoretically support this or that standard version can have bugs, it's better to rely on specific versions which are known to produce valid results.
In G++, you can do:
#if __GNUC__ == 4 && __GNUC_MINOR__ >= 9
// use C++11 features
#else if __GNUC__ == 5
// use C++11 features
#else
// maybe don't use some features
#endif
Just try to compile this program:
int main()
{
auto i = 0;
return 1;
}
using the command g++ -std=c++11 -c filename.cpp in your command line. auto is c++11 feature, so if that compiles with no errors, it means your compiler has support for C++11.
But in general, for gcc, before version 5.1, support for C++11 was experimental. So not until version 5.1 that gcc supported everything C++11 has to offer.

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.

Why does clang++ lack forward list?

I wrote up a simple C++ program that relies on forward_list like
#include <forward_list>
#include <iostream>
int main() {
std::forward_list<int> my_list;
my_list.push_front(3);
std::cout << my_list.top() << std::endl;
return 0;
}
However, when I compile this program on my Mac with clang++ my_program.cpp -std=c++11 -o my_program, I get this error:
my_program.cpp:1:14: fatal error: 'forward_list' file not found
#include <forward_list>
^
1 error generated.
Why can't clang find forward_list? Other C++11 features are working. For instance, the auto keyword works, albeit a warning appears that tells me that auto is a C++11 feature.
By default clang++ uses an older gcc-4.2 std library which has no C++11 support. You can tell clang to use a C++11-aware std::lib with the command -stdlib=libc++. libc++ has <forward_list>.

Resources