Posts

Showing posts with the label gcc

Why this code fails to compile with gcc 4.8.5 while it compiles fine with clang

Why this code fails to compile with gcc 4.8.5 while it compiles fine with clang #include<iostream> using namespace std; int main() { const int k = 10; // Capture k by value auto myl = ...

Compiler error : Only Copy elision is wanted, but move constructor seems to be required (by compiler)

Compiler error : Only Copy elision is wanted, but move constructor seems to be required (by compiler) Attempting to compile to this code give a compiler error with 'newer' compilers (im guessing: that support move constructors). Something along the lines of "attempting to call deleted function". Tried with gcc 8.1, MSVC 19.10 and clang 6.0.0 #include <string> #include <iostream> #include <sstream> class A : public std::stringstream { public: A(std::string str) : str_(str) { } //A(A&&); ~A() { std::cout << str_; } std::string str_; }; A make_A() { return A("hello"); } int test(int num) { A test = make_A(); } The 'A' class is a simply 'exploit' of copy-elision (or RVO - Return Value Optimization) and the fact that this doesn't invoke the custom destructor either. Surprisingly, commenting in the declaration of A's move constructor, makes the code both compile AND link. So it could ma...

Why does this code trigger gcc's “maybe uninitialized” warning?

Why does this code trigger gcc's “maybe uninitialized” warning? In plenty of other instances, gcc seems to be able to detect that the condition at the beginning of the loop initializes the variable. It even works if I drop the increment operator. It also goes away without the -02 flag. I've learned to trust that compiler warnings usually do mean something is wrong, so I'm wondering if there's something I'm missing or if it's just a weird compiler quirk. -02 void act(char **); void test(int width, int height){ char * rowA[width]; char ** A; for (int i = 0; i < width * height; ++i){ if (!(i % width)){ if (i){ act(rowA); } rowA[0] = 0; A = rowA; } *A++ = "hello"; } } Compiling on gcc-6.3.0 with -Wall -Werror -02 gcc-6.3.0 -Wall -Werror -02 Edit: to avoid confusion I've altered the code to be closer to the actual use case. ...

Cygwin warning command

Cygwin warning command I am using Cygwin to compile c++ programs, but I need to know what command do I use to show warnings when there is no endline or new line in the program. I tried using commands like -Wall, -Pedantic, etc but nothing shows up. Below is the current cygwin I am using. You mean when the *.cpp file does not end with a newline? This used to be technically illegal, but few compilers cared, and it has been legal since C++11. – aschepler 2 days ago Yea, when the *.cpp file does not end with a new line. I am trying to see if it can give me a warning when I don't add a newline. – ek9 2 days ago You will have to write a shell sc...