Different error on native compiler vs web compiler - c++

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.

Related

Ranges algorithm in LLVM 14 libc++

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.

clang standard library bug or c++ undefined behavior?

Does the following C++ program contain any undefined behavior?
int
main()
{
struct entry
{
uint32_t hash;
uint32_t idx;
};
entry arr[31] = {
{ 7978558, 0}, { 9241630, 1}, { 65706826, 2},
{ 639636154, 3}, {1033996244, 4}, {1225598536, 5},
{1231940272, 6}, {1252372402, 7}, {2019146042, 8},
{1520971906, 9}, {1532931792, 10}, {1818609302, 11},
{1971583702, 12}, {2116478830, 13}, { 883396844, 14},
{1942092984, 15}, {1274626222, 16}, { 333950222, 17},
{1265547464, 18}, { 965867746, 19}, {1471376532, 20},
{ 398997278, 21}, {1414926784, 22}, {1831587680, 23},
{ 813761492, 24}, { 138146428, 25}, { 337412092, 26},
{ 329155246, 27}, { 21320082, 28}, {1751867558, 29},
{1155173784, 30},
};
std::sort(std::begin(arr), std::end(arr),
[](entry a, entry b) { return a.hash <= b.hash; });
}
When I compile with gnu c++ compiler or any clang/llvm after 12.0.0, the program works fine. However, when I compiled it with clang version 12.0.0 (the default compiler shipped on my Mac laptop), it crashed inside of std::sort() as following:
$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.2)
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
$ g++ -g -std=c++11 bug.cc
$ ./a.out
Segmentation fault: 11
Also if I change it to use std::vector instead of fixed size array. The std::sort() will never return when compiled with clang 12.0.0
Yes, the comparator is not a strict weak ordering which violates the preconditions of std::sort, resulting in undefined behavior.
For two arguments a and b (possibly identical), a strict weak ordering comp should never evaluate both comp(a,b) and comp(b,a) to true. In other words, it should model the behavior of the built-in <, not <=.
So in your code it should be <, not <=, to make it a strict weak ordering.

Initialising vector in C++

I'm initialising vector using the below method
vector<int> num1{1, 9, 3, 7, 0, 7, 7, 2, 1};
and it is throwing error as below,
Multiplication.cpp:29:18: error: expected ';' at end of declaration
vector<int> num1{1, 9, 3, 7, 0, 7, 7, 2, 1};
^
You haven't included the vector library,
just add
#include<vector>
at the top of your code
it is running check here
if you did that too, then too,
it is a compiler problem, try using a different complier or online complier like ideone

make_heap and pop_heap are OK but push_heap not

I just came across a weird problem that happens ONLY on MSVC with Clion but not on other compilers(I tried gcc on Linux and Visual Studio both no such problem, with the same code).
With these codes:
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int>v = {1,2,3,4,5};
make_heap(v.begin(), v.end());
v.push_back(6);
push_heap(v.begin(), v.end());
}
an error "In instantiation of function template specialization 'std::push_heapstd::_Vector_iterator<std::_Vector_val<std::_Simple_types<int > > >' no type named 'value_type' in 'std::indirectly_readable_traitsstd::_Vector_iterator<std::_Vector_val<std::_Simple_types<int > > >'" will then be shown
is it a bug of Clion or MSVC?
P.S.
I can still build and run it so it might not be a compiler error; (Making me even more confused)
It looks like you cannot intialize vector with the following command:
vector<int>v = {1,2,3,4,5};
Change it to:
vector<int> vect{ 1, 2, 3, 4, 5 };
Compile and run the code and see if it still has problems.
EDIT:
Some people are saying it is unlikely however look at the link:
What is the easiest way to initialize a std::vector with hardcoded elements?
If you scroll down to the second answer it says:
If your compiler supports C++11, you can simply do:
std::vector<int> v = {1, 2, 3, 4};
As you did not tell us your compiler version and environment it is very hard to determine if this is the problem. Also note that:
This is available in GCC as of version 4.4.
Unfortunately, VC++ 2010 seems to be lagging behind in this respect.
So if you are using an older version of VC++ then you are out of luck...

Does clang on Mac not support uniform initialization?

Is clang on Mac not support uniform initialization?
I tried compiling following code, But compiler raise a error.
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<int> v = {3, 1, 9, 4};
std::cout << v[1] << std::endl;
}
Error:
vector.cpp:9:22: error: non-aggregate type 'std::vector<int>' cannot be initialized with an initializer list
std::vector<int> v = {3, 1, 9, 4};
^ ~~~~~~~~~~~~
1 error generated.
OS: macOS 10.12.4
compiler version:
Apple LLVM version 8.1.0 (clang-802.0.42)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
I suspect you are not compiling the code as C++11/14 (-std=c++11 or -std=c++14)? If not; do that. Clang does support what you are trying to do.