Can't compile C++ 17 structured bindings - c++

The following code
#include <iostream>
#include <tuple>
int main()
{
auto [i, c, d] = std::make_tuple(1, 'a', 2.3);
std::cout << "i=" << i << " c=" << c << " d=" << d << '\n';
return 0;
}
doesn't get compiled on my computer. I get these error messages:
error: use of undeclared identifier 'i'
error: expected unqualified-id
and some more of the same type.
I'm using: Mac OS X 10.11.6 El Capitan, CLion. I did choose the C++ 17 option when I created the project and my CMakeList.txt has this line:set(CMAKE_CXX_STANDARD 17).
clang --version - Apple LLVM version 8.0.0 (clang-800.0.42.1)
What do I need to do to compile this code?

As #Eljay said in the comments, older versions of clang did (do) not have complete C++17 support.
I have reproduced this issue w/o CLion.
On a 10.11.6 machine, using "Apple LLVM version 8.0.0 (clang-800.0.42.1)"
clang++ -std=c++1z junk.cpp
gives the errors that the OP reported. (Note that -std=c++17 is not a valid option here - that came later)
On a 10.14.2 machine, using "Apple LLVM version 10.0.0 (clang-1000.10.44.4)"
clang++ -std=c++17 junk.cpp
compiles w/o error.

Related

Are there any versions of clang that have complete implementations of std::ranges?

I'm just trying to figure out how I can use clang++ to compile this:
for (int i : std::ranges::iota_view{1, 10})
std::cout << i << ' ';
The latest Apple Clang failed to recognize iota_view, and so I downloaded LLVM 15.0.7 using Homebrew. This suggests that 15.0.7 is compatible with ranges, but I'm still having trouble with it not being recognized, even with the -fexperimental-library, -std=c++20, and -stdlib=libc++ flags. Anyone know how to resolve this, short of just using gcc instead?
It works with LLVM experimental C++ standard library.
#include <ranges>
#include <iostream>
#include <algorithm>
int main() {
for (int i : std::ranges::iota_view(1, 10))
std::cout << i << ' ';
std::cout << '\n';
}
clang main.cc -std=c++20 -stdlib=libc++ -fexperimental-library
./a.out
1 2 3 4 5 6 7 8 9
https://godbolt.org/z/5frq61K4Y
See also libc++ C++20 Status.

Apple's clang can't use <=> with std::tuple

The following compiles fine with GCC and clang on on godbolt, but on my MacBook, in Xcode 14 it dies:
#include <iostream>
#include <compare>
#include <tuple>
using std::cout; using std::tuple; using std::endl;
int main() {
tuple<float, float> tuplee = {1.0,2.0};
tuple<float, float> tuploo = {3.0,4.0};
cout << (tuplee < tuploo) << endl;
auto res = (tuplee <=> tuploo);
cout << (res < 0) << endl;
return 0;
}
The error is:
invalid operands to binary expression ('std::tuple<float, float>' and 'std::tuple<float, float>')
It points to the <=> on the tuples. Do you think it's a bug in Apple's clang, or am I missing something?
Command line on my MacBook:
% clang++ --version
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
% clang++ -std=c++2b main.cpp
main.cpp:11:21: error: invalid operands to binary expression ('tuple<float, float>' and 'tuple<float, float>')
cout << (tuplee <=> tuploo) << endl;
~~~~~~ ^ ~~~~~~
1 error generated.
I think it is a bug. The bug was fixed in llvm (relevant change). But by checking the tuple header in Macos SDK, one can find apple do not implement <=> for tuple.
The bug also affects arm64 variants of Macos. Clang version on my mac:
➜ test clang --version
Apple clang version 14.0.0 (clang-1400.0.29.202)
Target: arm64-apple-darwin22.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
PS. Homebrew llvm#14 compiles fine. Just do not use apple clang

Compiler error with filesystem library: clang and g++

I am writing a personal project in c++ which needs to access to files in some directories, hence I decided to use the filesystem library. I encountered some problems when I try to compile my project on MacOS and on Linux.
The code snippet is the following
#include <iostream>
#include <fstream>
int main(){
std::string path = "Inner";
std::cout << "Files in " << path << " directory :" << std::endl;
for (const auto & entry : std::filesystem::directory_iterator(path))
std::cout << entry.path() << std::endl;
return 0;
}
When I compile it on my MacBook Pro (clang version 11.0.3 (clang-1103.0.32.62)) with
g++ -o test test.cpp -std=c++17 -Wall
everything works fine. But as soon as I move to Linux (Ubuntu 19.04, g++ 8.3.0) I get the following error:
test.cpp: In function ‘int main()’:
test.cpp:8:33: error: ‘std::filesystem’ has not been declared
for (const auto & entry : std::filesystem::directory_iterator(path)){
I include then the filesystem library with #include <filesystem>:
#include <iostream>
#include <fstream>
#include <filesystem>
int main(){
std::string path = "Inner";
std::cout << "Files in " << path << " directory :" << std::endl;
for (const auto & entry : std::filesystem::directory_iterator(path))
std::cout << entry.path() << std::endl;
return 0;
}
compile it via g++ -o test test.cpp -std=c++17 -Wall -lstdc++fs and everything works fine on Linux too (note that I had to add -lstdc++fs).
Why is there this different behaviour on MacOS and on Linux? Does it depends on the compiler? What happens with Windows OS (I do not have any Windows PC at home)?
I found a related question and its answer here, but it does not seem to explain why in the first case (with clang) everything works fine also without including filesystem library.
Using 'g++' is not using clang you should use 'clang++'
Gcc should not be platform dependent but it might be different version
At any case, you should explicitly include header files needed, and std::filesystem is defined in "<filesystem>"
regarding the need to add "lstdc++fs' - this is a hint that actually g++ version is different and uses different llvm versions. As described in https://en.cppreference.com/w/cpp/filesystem
Notes:
Using this library may require additional compiler/linker options. GNU implementation prior to 9.1 requires linking with -lstdc++fs and LLVM implementation prior to LLVM 9.0 requires linking with -lc++fs

CUDA 6.5 with g++ does not support c++11?

I read it here that CUDA 6.5 has started support for C++11 :
https://groups.google.com/forum/#!topic/thrust-users/R37GIkMG4tk
But when I compile an example code below, I got
$ nvcc -std=c++11 cu-gcc11.cu -o test
nvcc warning : The -c++11 flag is not supported with the configured host compiler. Flag will be ignored.
cu-gcc11.cu(7): error: explicit type is missing ("int" assumed)
My setting : CUDA 6.5, g++ 4.5, ubuntu 12.04
Codes :
#include <cuda.h>
#include <iostream>
__host__ void test() {
float a = 12.;
double b = 3.;
auto c = a * b;
std::cout << c << std::endl;
}
int main()
{
test();
return 0;
}
C++11 support in nvcc is experimental at this time. In order to properly use it you will need an appropriate configuration. This is not documented anywhere AFAIK, but you should have good results with either Fedora 20 or Ubuntu 14.04, both of which are supported configs for cuda 6.5 and include GCC 4.8.x.
In your case your GCC version is just too old.
I don't think -std=c++11 was available in GCC 4.5. Try -std=c++0x.

Error when compiling gcc 4.6.1 C++0x threading code on MacOSX Lion

When compiling the following code:
#include <iostream>
#include <thread>
using namespace std;
void hello()
{
cout << "Hello World!" << endl;
}
int main()
{
cout << "starting" << endl;
thread t(hello);
t.join();
cout << "ending" << endl;
return 0;
}
using:
$ g++-4.6.1 -std=c++0x -pthread threading.cpp
I get the following error:
threading.cc: In function ‘int main()’:
threading.cc:13:2: error: ‘thread’ was not declared in this scope
threading.cc:13:9: error: expected ‘;’ before ‘t’
threading.cc:14:2: error: ‘t’ was not declared in this scope
This is on MacOSX Lion with a custom built gcc 4.6.1. All the other c++0x features that are valid for gcc 4.6 works like a charm. Is this a MacOSX specific error?
std::thread (and the rest of the C++11 thread library) is only available for some of the platforms supported by gcc 4.6.1. Unfortunately for you, MacOSX is not one of those platforms.
My commercial Just::Thread library provides the C++11 thread facilities for 32-bit MacOSX with gcc 4.5, but gcc 4.6 is not supported yet.
See http://gcc.gnu.org/PR50196 - Mac OS X doesn't support some parts of pthreads that we rely on. Building the latest version won't help, but it might be fixed for GCC 4.7