g++

Optimizing the number of constructor calls

徘徊边缘 提交于 2019-12-21 07:50:51
问题 At work we have a class with an expensive constructor so we would like it to be called as few times as possible. We looked through the uses of it and tried to make the code more RVO friendly so to say. However we found a quirk in the g++ compiler where we didn't understand what happened. Please consider the two implementations of operator+ const Imaginary Imaginary::operator+(const Imaginary& rhs) const { Imaginary tmp(*this); tmp.append(rhs); return tmp; } and const Imaginary Imaginary:

clang bug? namespaced template class' friend

南楼画角 提交于 2019-12-21 07:21:40
问题 The following code which doesn't compile under clang but does under gcc and VS: template<typename T> class bar; namespace NS { template<typename T> class foo { foo() {} template<typename U> friend class bar; }; } template<typename R> class bar { public: bar() { NS::foo<int> f; } }; int main(int, char **) { bar<int> b; return 0; } It fails with: main.cpp:20:22: error: calling a private constructor of class 'NS::foo<int>' NS::foo<int> f; ^ main.cpp:8:9: note: implicitly declared private here

clang bug? namespaced template class' friend

吃可爱长大的小学妹 提交于 2019-12-21 07:21:03
问题 The following code which doesn't compile under clang but does under gcc and VS: template<typename T> class bar; namespace NS { template<typename T> class foo { foo() {} template<typename U> friend class bar; }; } template<typename R> class bar { public: bar() { NS::foo<int> f; } }; int main(int, char **) { bar<int> b; return 0; } It fails with: main.cpp:20:22: error: calling a private constructor of class 'NS::foo<int>' NS::foo<int> f; ^ main.cpp:8:9: note: implicitly declared private here

Is there a way to detect inline function ODR violations?

我只是一个虾纸丫 提交于 2019-12-21 07:14:16
问题 So I have this code in 2 separate translation units: // a.cpp #include <stdio.h> inline int func() { return 5; } int proxy(); int main() { printf("%d", func() + proxy()); } // b.cpp inline int func() { return 6; } int proxy() { return func(); } When compiled normally the result is 10 . When compiled with -O3 (inlining on) I get 11 . I have clearly done an ODR violation for func() . It showed up when I started merging sources of different dll's into fewer dll's. I have tried: GCC 5.1 -Wodr

GCC NRVO/RVO warning

烂漫一生 提交于 2019-12-21 07:13:10
问题 Is there any warning , which allows us to know whether NRVO/RVO performed or not, in GCC ? I found that -fno-elide-constructors turns off NRVO/RVO , but NRVO/RVO has its own conditions to occur and sometimes does not occur. There is a need to know if NRVO/RVO occurs to understand, when extra copy-construction happens. I am especially interested in compile-time features. It would be nice if there were some specific #pragma GCC... (which activates the diagnostic immediately following itself) or

GCC doesn't like C++ style casts with spaces [duplicate]

旧巷老猫 提交于 2019-12-21 06:23:35
问题 This question already has an answer here : Constructor-style casting in function call parameters (1 answer) Closed 5 years ago . I am porting some C++ code to GCC, and apperantly it isn't happy with C++ style casting when sapces are involved, as in unsigned int(-1) , long long(ShortVar) etc... It gives an error: expected primary-expression before 'long' . Is there any way to make peace with GCC without going over each one of those and rewrite in c-style? 回答1: GCC is correctly crying --

main.cpp:(.text+0x5f): undefined reference to

[亡魂溺海] 提交于 2019-12-21 06:21:00
问题 I try to compile some exercise from a SDL guide. I compile like this: g++ -o main main.cpp -I/usr/local/include/SDL2 -L/usr/local/lib -lSDL2 and i get this: /tmp/cci2rYNF.o: In function `main': main.cpp:(.text+0x5f): undefined reference to `Game::init(char const*, int, int, int, int, int)' collect2: error: ld returned 1 exit status and my code is: main.cpp #include "Game.h" // our Game object Game* g_game = 0; int main(int argc, char* argv[]) { g_game = new Game(); g_game->init("Chapter 1",

g++ flag to only check syntax?

守給你的承諾、 提交于 2019-12-21 06:06:38
问题 Is there a way to have g++ check for C++98 syntax when compiling but at the same time compile as if no -std= has been given ? My goal is to make sure that my source code stays C++98 but I don't want to prevent g++ from using any newer optimisation or trick. For the moment, I compile my projet twice, once with CXXFLAGS=-std=c++98 and one with a final empty CXXFLAGS for release. It looks like gcc 5 will have -Wc90-c99-compat and -Wc99-c11-compat , that something in that direction. 回答1: You will

Am I using the pointer class properly in this generic unique_ptr<>() deleter?

烈酒焚心 提交于 2019-12-21 05:35:08
问题 I created a generic deleter template that can be used to create unique_ptr<>() sub-types allowing for a Deleter other than just delete ptr . It works great with the default optimization flags (i.e. -O0 ), however, when I use -O3 the T & operator * () function, somehow, returns 0 instead of the f_pointer contents. I would like to make sure that we agree that there is something wrong in the compiler and that my template is correct. The following is a complete piece of code that should compile

How to disable all warnings in g++ on a few lines of code

久未见 提交于 2019-12-21 05:06:21
问题 How to disable all warnings on a few lines of code. Specific warnings can be disabled using GCC diagnostic feature, but is there a flag for all warnings. I tried this way but it doesn't work #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-wall" // some code #pragma GCC diagnostic pop 回答1: From here: http://dbp-consulting.com/tutorials/SuppressingGCCWarnings.html For version 4.6 or later, you can save the state of the user's diagnostic flags. You can insert this around the line