Why can't I compile structured bindings? - c++

I was trying to compile this code (in the file test.cpp)
#include<tuple>
int main(){
auto [c,d] = make_tuple(3.1,2.3);
}
using
g++ -std=c++17 test.cpp -o test
, as well as
clang++ -std=c++1z test.cpp -o test
both would print the error message:
test.cpp: In function ‘int main()’:
test.cpp:3:7: error: expected unqualified-id before ‘[’ token
auto [c,d] = make_tuple(3.1,2.3);
using
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
and
clang version 3.8.0-2ubuntu4
(using Ubuntu 16.04.09)
What am I missing?

From the official website of GCC:
Structured bindings is supported in gcc version 7
From the official website of clang:
Structured bindings is supported in version 4.
The following link presents compiler support for new C++ features. https://en.cppreference.com/w/cpp/compiler_support

Related

Compile with c++17 mac

I can't compile with -std=c++17, I got :
error: invalid value 'c++17' in '-std=c++17'
However I update Xcode and clang.
My Clang version is:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin`
And I load the newest header like optional, I have to do
#include <experimental/optional>
instead of
#include <optional>
Xcode brings its own complete toolchain, including headers and the actual compiler.
Apple LLVM version 9.0.0 (clang-900.0.39.2) (which ships with Xcode 9.2) does not support the usage of the flag -std=c++17 since its too old. The optional header is only included under the folder experimental/. Which is why you need to #include <experimental/optional>
In order to compile your program with c++17 support using the compiler which comes with Xcode 9.2 you need to use the -std=c++1z flag.
Xcode 9.3 will be shipped with Apple LLVM version 9.1.0 (clang-902.0.30) which has support for the -std=c++17 flag. However the optional header is as of today still under the experimental/ subdirectory. This might change during the betas.
Here is what I get with this tests:
#include <experimental/optional>
int main(int, char* []) {
return 0;
}
g++ -std=c++17 -o test test.cpp
error: invalid value 'c++17' in '-std=c++17'
g++ -std=c++1z -o test test.cpp
Did you try the c++1z argument?
Also of note my test compiles without the -std=c++1z argument provided.
I think I'm on a newer version of OSX than you:
Target: x86_64-apple-darwin17.4.0
You should use -std=c++1z as flag.
libc++ with c++17 support since macos 15
-std=c++1z also works on Apple LLVM version 8.1.0 (clang-802.0.42)

How to use the available C++17 flags from Xcode in Terminal

My Xcode 9.2 has the option to compile C++ using: -std=c++17 or -std=gnu++17.
My C++ code that has some C++17 features compiles successfully.
However, if I try to compile the same code in my Terminal using the same flags, I get the following errors:
clang++ -std=c++17 test.cpp -o test
error: invalid value 'c++17' in '-std=c++17'
and
clang++ -std=gnu++17 test.cpp -o test
error: invalid value 'gnu++17' in '-std=gnu++17'
My OSX version is 10.13.3 and my Clang++ version is:
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.4.0
I thought that Terminal would use the same compiler from Xcode. That may be the case, but I can't use the same options. At least I couldn't.
I tried using -std=C++1z, but it didn't compile my code because it doesn't understand some new features from C++17.
So my question is: Is there a way to use -std=c++17 or -std=gnu++17 that are currently available in my Xcode 9.2, in my Terminal app?
This questions is not the same as the others available at stackoverflow because all the answers I found the -std=c++17options was not available in Xcode. Now we have this options, but I can't figure out how to use it in Terminal.
The compiler Apple currently ships with Xcode 9.2 (clang-900.0.39.2) does not support the -std=c++17 flag.
Xcode uses the -std=c++1z flag when you enable c++17 support. If you want to use -std=c++17 you need to manually install clang. You could do that using brew install llvm (assuming you have homebrew installed).
You can compile your program with
/usr/local/Cellar/llvm/5.0.0/bin/clang++ -std=c++17 test.cpp -o test
You change the symlinks in usr/bin/clang++ to point at the new destination if you don't want to use the full path to the compiler.
The compiler which is shipped with Xcode 9.3 will be
Apple LLVM version 9.1.0 (clang-902.0.30)
This can handle the -std=c++17 flag.

clang++ only compiles C++11 program using boost::format when -std=c++11 option is dropped

Please take a look at the following C++11 snippet:
#include <boost/format.hpp>
int main(int argc, char** argv)
{
auto s = boost::format("");
return 0;
}
When I compile it with clang using the -std=c++11 I get the following error:
$ clang++ -std=c++11 -o main main.cpp
In file included from main.cpp:1:
In file included from /usr/include/boost/format.hpp:19:
In file included from /usr/include/boost/detail/workaround.hpp:41:
In file included from /usr/include/boost/config.hpp:40:
In file included from /usr/include/boost/config/select_stdlib_config.hpp:18:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/cstddef:51:11: error:
no member named 'max_align_t' in the global namespace
using ::max_align_t;
~~^
1 error generated.
Without the -std=c++11 everything compiles fine, but clang prints a warning:
$ clang++ -o main main.cpp
main.cpp:5:3: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto s = boost::format("");
^
So, it looks like a valid workaround is to drop the C++11 flag, as the current version of clang seem to be in C++11 mode, anyway? The drawback is that you will get many warnings.
Is there a better workaround beside completely switching to gcc? Patching the source code of boost::format or gcc-libs is fine for me.
System information:
Platform: Arch Linux x86_64
Boost version: 1.55.0-6
gcc-libs: 4.9.0-1
clang++: 3.4 (tags/RELEASE_34/final)
The bug is closed now. It should be fixed in Arch with clang 3.4-2.
With this commit, Evangelos Foutras merged the following patch from upstream:
http://reviews.llvm.org/rL201729

cross compile c++ on linux to windows

clang++ main.cpp -std=c++11 -target x86_64-unknown-win32 -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/
main.cpp:6:10: fatal error: 'string' file not found
#include <string>
^
1 error generated.
that's weird because:
/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/ssp$ ls
ssp.h stdio.h string.h unistd.h
and this result the same:
clang++ main.cpp -std=c++11 -target x86_64-unknown-win32 -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include/ssp
well. I used to compile c code from linux to windows just fine using:
x86_64-w64-mingw32-gcc main.c
and I tried:
x86_64-w64-mingw32-g++ -std=c++0x main.cpp
also:
x86_64-w64-mingw32-g++ -std=c++0x main.cpp -I/usr/lib/gcc/x86_64-w64-mingw32/4.6/include
still give bunch of errors like
main.cpp:70:23: sorry, unimplemented: non-static data member initializers
main.cpp:70:23: error: in-class initialization of static data member ‘origin’ of non-literal type
main.cpp: In constructor ‘Item::Item(std::string)’:
main.cpp:83:18: error: ‘stoul’ is not a member of ‘std’
main.cpp:88:4: error: ‘origin’ was not declared in this scope
main.cpp:89:17: error: ‘stof’ is not a member of ‘std’
main.cpp: In function ‘std::ostream& operator<<(std::ostream&, const Item&)’:
....
x86_64-w64-mingw32-g++ (GCC) 4.6.3 seems to be the latest version on ubuntu repo.
I'm at lost..why the hell clang won't use the headers. why the hell x86_64-w64-mingw32-g++ won't listen that it should use c++11 standards.
The GCC version you use does not support non-static data member initializers as shown here. Find out here which version of GCC supports the C++11 feature set you required.

Activate Smart Pointers?

I wanted to play around with the new features of C++11, namely with Smart Pointers. I found an exampleg++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2 here: Cplusplus11-Smart-Pointers:
#include <memory>
int main() {
std::shared_ptr<int> sptr1( new int );
}
When I try it out is everything I get:
In function ‘int main()’:|
error: ‘shared_ptr’ was not declared in this scope|
error: expected primary-expression before ‘int’|
error: expected ‘;’ before ‘int’|
This is my g++ Version:
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Do I have to "activate" C++11 at first?
You need to pass the -std=c++11 compiler flag to the g++ compiler. CodeBlocks allows you to configure this via
Settings -> Compiler -> Compiler Settings
Note for older versions of gcc, you may need -std=c++0x.