g++

uninitialized const

余生颓废 提交于 2019-12-29 04:15:31
问题 This compiles perfectly fine with the current MSVC compiler: struct Foo { } const foo; However, it fails to compile with the current g++ compiler: error: uninitialized const 'foo' [-fpermissive] note: 'const struct Foo' has no user-provided default constructor If I provide a default constructor myself, it works: struct Foo { Foo() {} } const foo; Is this another case of MSVC being too permissive, or is g++ too strict here? 回答1: The C++03 Standard: 8.5 [dcl.init] paragraph 9 If no initializer

Cross-platform ALIGN(x) macro?

▼魔方 西西 提交于 2019-12-29 03:37:12
问题 I would like to create a struct that has a certain alignment. I would like to use the same struct definition for both GCC and VisualC++ compilers. In VisualC++, one typically does this: __declspec(align(32)) struct MyStruct { // ... }; In GCC, one typically does this: struct MyStruct { // ... } __attribute__ ((aligned (32))); I could of course create the appropriate macros to make this work: BEGIN_ALIGNED_STRUCT(32) struct { // ... } END_ALIGNED_STRUCT(32) ; And thus be able to handle both

How to copy a string into a char array in C++ without going over the buffer

眉间皱痕 提交于 2019-12-29 03:20:09
问题 I want to copy a string into a char array, and not overrun the buffer. So if I have a char array of size 5, then I want to copy a maximum of 5 bytes from a string into it. what's the code to do that? 回答1: First of all, strncpy is almost certainly not what you want. strncpy was designed for a fairly specific purpose. It's in the standard library almost exclusively because it already exists, not because it's generally useful. Probably the simplest way to do what you want is with something like:

Missing const_iterator overload of std::vector::erase() with g++ 4.8

北战南征 提交于 2019-12-28 20:38:33
问题 The following example will not compile using g++ 4.8.2: #include <iostream> #include <vector> using namespace std; int main() { vector<int> v {1, 2, 3}; v.erase(v.cbegin()); // Compiler complains return 0; } The compiler says the following. (It isn't very readable, but it's complaining that there's not a known conversion between vector<int>::const_iterator and vector<int>::iterator .) prog.cpp: In function ‘int main()’: prog.cpp:8:20: error: no matching function for call to ‘std::vector<int>:

Use libraries compiled with visual studio in an application compiled by g++ (mingw)

旧街凉风 提交于 2019-12-28 14:00:37
问题 Is it possible to use a library compiled by visual studio in an application compiled by g++ (mingw) on Windows? 回答1: If the library is written in C++ and exposes a C++ interface: no (because the name-mangling differs between g++ and VC++). If the library is a static library written in C (or with an extern "C" interface): yes, but certain caveats apply. If the library is a DLL with a C interface: yes, but you'll have to create your own import library. 回答2: Also see the discussion for question

Static initialization and destruction of a static library's globals not happening with g++

巧了我就是萌 提交于 2019-12-28 12:14:45
问题 Until some time ago, I thought a .a static library was just a collection of .o object files, just archiving them and not making them handled differently. But linking with a .o object and linking with a .a static library containing this .o object are apparently not the same . And I don't understand why... Let's consider the following source code files: // main.cpp #include <iostream> int main(int argc, char* argv[]) { std::cout << "main" << std::endl; } // object.hpp #include <iostream> struct

std::enable_if : parameter vs template parameter

被刻印的时光 ゝ 提交于 2019-12-28 04:50:13
问题 I'm building some input checker that needs to have specific functions for integer and/or double (for example 'isPrime' should only be available for integers). If I'm using enable_if as a parameter it's working perfectly : template <class T> class check { public: template< class U = T> inline static U readVal(typename std::enable_if<std::is_same<U, int>::value >::type* = 0) { return BuffCheck.getInt(); } template< class U = T> inline static U readVal(typename std::enable_if<std::is_same<U,

How to forward declare a template class in namespace std?

血红的双手。 提交于 2019-12-28 03:20:11
问题 #ifndef __TEST__ #define __TEST__ namespace std { template<typename T> class list; } template<typename T> void Pop(std::list<T> * l) { while(!l->empty()) l->pop(); } #endif and used that function in my main. I get errors. Of course, I know that there are more template params for std::list (allocator I think). But, that is beside the point. Do I have to know the full template declaration of a template class to be able to forward declare it? EDIT: I wasn't using a pointer before - it was a

Why can't g++ find iostream.h?

痞子三分冷 提交于 2019-12-27 14:55:44
问题 I'm trying to understand how to compile C++ programs from the command line using g++ and (eventually) Clang on Ubuntu. I found a webpage which explains MakeFiles and I am following their directions. http://mrbook.org/tutorials/make/ I downloaded the four example files into their own directory. main.cpp hello.cpp factorial.cpp functions.h I then went ahead and ran their example of how to manually compile without a MakeFile. g++ main.cpp hello.cpp factorial.cpp -o hello When I ran the command

Why can't g++ find iostream.h?

若如初见. 提交于 2019-12-27 14:55:02
问题 I'm trying to understand how to compile C++ programs from the command line using g++ and (eventually) Clang on Ubuntu. I found a webpage which explains MakeFiles and I am following their directions. http://mrbook.org/tutorials/make/ I downloaded the four example files into their own directory. main.cpp hello.cpp factorial.cpp functions.h I then went ahead and ran their example of how to manually compile without a MakeFile. g++ main.cpp hello.cpp factorial.cpp -o hello When I ran the command