I have this snippet.
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};
return std::ranges::equal(v1, v2);
}
I compile it with GCC 10 (Debian stable) and everything's alright:
$ g++ -std=c++20 test.cpp -o test
<compiles fine>
I compile it with Clang 14 and libc++14 (Debian stable, installed from packages from apt.llvm.org):
$ clang++-14 -std=c++20 -stdlib=libc++ test.cpp -o test
test.cpp:8:25: error: no member named 'equal' in namespace 'std::ranges'
return std::ranges::equal(v1, v2);
~~~~~~~~~~~~~^
1 error generated.
Same for a lot of other things. Is libc++ support for the ranges library really so behind or am I missing something?
You can find an exhaustive table for implementations feature support here: https://en.cppreference.com/w/cpp/compiler_support
For C++20s "The One Ranges Proposal" where std::equal is part of the table says "13 (partial)".
There is another overview for clang here: https://clang.llvm.org/cxx_status.html#cxx20. Though it only lists language features.
Related
I have this piece of code summing std::valarray<int>'s:
#include <iostream>
#include <valarray>
#include <vector>
int main()
{
std::vector<std::valarray<int>> vectorOfValarrays{{1, 1}, {2, 2}, {3, 3}};
std::valarray<int> sumOfValarrays(2);
for (const auto& i : vectorOfValarrays)
sumOfValarrays = sumOfValarrays + i;
std::cout << sumOfValarrays[0] << ' ' << sumOfValarrays[1];
}
Compiling with x86-64 gcc 12.2 using -O0 and -O1, it prints the expect result:
6 6
But when compiling with -O2 and -O3, it prints:
3 3
What could be the reason for this? Is my code undefined behaviour or is this a gcc bug?
I'm pretty sure that this is a gcc bug. Clang gives the correct behaviour for all optimization levels (-O0, -O1, -02, -O3). I also have a look at std::valarray constructors, operator + and operator = and it seems like my code doesn't any undefined behaviour.
I found this bug report on gcc Bugzilla, and the problem seems like gcc has the wrong implementation for copying std::valarray, so in the question the line sumOfValarrays = sumOfValarrays + i; makes gcc tripped up.
I'm having a strange issue, and being new to c++ isn't helping. I wrote the following to make a simple 2D matrix:
#include <iostream>
#include <vector>
std::vector< std::vector<int> > grid(){
std::vector< std::vector<int> > vect;
vect = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
return vect;
}
When I run this on a site like cpp.sh, it works perfectly fine, but when I use g++ on my macbook, I get the following error:
username % g++ main.cpp
main.cpp:6:13: error: expected expression
vect = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
^
1 error generated.
I've updated my gcc, and I've run with both g++ and gcc – both have the same issue. I installed gcc with homebrew and i've just been running g++ main.cpp and then executing a.out.
Note: I'm on MacOS Monterey on M1
Thanks for the comments! For anyone else having similar issues:
On macOS Monterey gcc/g++ (by default) uses the 1998 version, where my code requires the 2011+ versions.
To compile with the newer version, #wlk showed that I could use clang++ -std=c++11 to specify the version.
By adding alias g++="clang++ -std=c++20" to my .zshrc file, I can run g++ with the 2020 version – without issue.
This question already has answers here:
Lambda capture and parameter with same name - who shadows the other? (clang vs gcc)
(2 answers)
Closed 8 months ago.
This code compiles with g++, no collision is detected, although s is the parameter captured by lambda and the lambda parameter. My compiler is g++.
gcc Version is
gcc (Debian 4.9.2-10+deb8u2) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
and I am using these compilation flags :
-W
-Wall
-ansi
-pedantic
-s
-march=native
-flto
-fwhole-program
-Wfatal-errors
-Wextra
-std=c++1y
-frename-registers
-fipa-pta
-Ofast
-pedantic-errors
-fira-loop-pressure
-fomit-frame-pointer
-fforce-addr
-falign-functions
-fno-cprop-registers
-fstrength-reduce
Code is
#include <vector>
#include <algorithm>
#include <iostream>
int main (int argc, char* argv []) {
int s (0);
std::vector<int> vec ({3, 5, 13, 1});
std::for_each (vec.begin (), vec.end (), [&s] (const int& s) {
s = s>s?s:s;
});
std::cout << "max ox v is " << s << std::endl;
}
Is there a compilation option to detect this kind of error ?
Upgrade your compilers.
Based on https://godbolt.org/z/h3Y7dW1dP that warning is missing for gcc 8.5 (with -std=c++17) and clang 7.1.0.
You should upgrade to gcc 9.1 and clang 8.0.0 or later. (Preferably a lot later.)
I ran the following code
vector<int> randomIntegers = generateIntegers(10); // Generates 10 integers
std::ranges::sort(randomIntegers);
When I compile with g++ -std=c++20 file.cpp , I get
error: 'sort' is not a member of 'std::ranges'; did you mean 'std::sort'?
gcc --version: gcc 10.2.0
g++ --version: g++ 10.2.0
Why is sort not a member? I'm using VScode intellisense, and it shows methods such as advance,begin,common_view. But not sort.
To get access to std::ranges::sort you need to #include <algorithm>:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> randomIntegers{9,8,7,6,5,4,3,2,1,0}; // some integers
std::ranges::sort(randomIntegers);
}
Demo
ranges api
However you can use sort as follows:
#include <algorithm>
std::sort(randomIntegers.begin(), randomIntegers.end());
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.