c++03

error: anachronistic old-style base class initializer

那年仲夏 提交于 2019-12-03 06:28:39
问题 The following code produces the subsequent compilation error on all versions of GCC that I've tried, in C++98, C++11 and C++14 modes: struct T { T(void* x) : (x) {} }; // main.cpp: In constructor 'T::T(void*)': // main.cpp:3:18: error: anachronistic old-style base class initializer [-fpermissive] // T(void* x) : (x) {} // ^ // main.cpp:3:16: error: unnamed initializer for 'T', which has no base classes // T(void* x) : (x) {} Sure, it's clearly broken code because I'm not actually initialising

C++, is set_terminate local to every thread?

ⅰ亾dé卋堺 提交于 2019-12-03 06:03:20
Should set_terminate / get_terminate set a different terminate exception processor for several threads in C++ 2011 or C++ 2003? E.g. if I have program and sets terminate handler to func_1 ; then I start 3 threads. What are terminate handlers in new threads? What if in every thread I will set terminate handler to func_2 in first thread, func_3 in second thread and so on. N3242 (C++ 2011 draft) says nothing about it in [handler.functions] or in [support.exception] / [exception.terminate] PS: You may answer for C++2011 or for C++2003 of for any popular implementation of these standards PPS: There

Does a reference declaration introduce a new name for the referent?

北战南征 提交于 2019-12-03 04:45:53
In this question we've learnt that RVO cannot be applied to an expression like p.first . In comments it was also suggested that RVO is generally not applied to an expression like r after a declaration like auto& r = p.first . It is less clear whether the standard mandates this behaviour. in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function parameter or a variable introduced by the exception-declaration of a handler ([except.handle])) with the same type (ignoring cv-qualification) as the function

C++ Matrix multiplication type detection

人走茶凉 提交于 2019-12-02 18:34:54
问题 In my C++ code I have a Matrix class, and some operators written to multiply them. My class is templated which mean I can have int, float, double ... matrices. My operator overload is classic I guess template <typename T, typename U> Matrix<T>& operator*(const Matrix<T>& a, const Matrix<U>& b) { assert(a.rows() == b.cols() && "You have to multiply a MxN matrix with a NxP one to get a MxP matrix\n"); Matrix<T> *c = new Matrix<T>(a.rows(), b.cols()); for (int ci=0 ; ci<c->rows() ; ++ci) { for

Is member value in the class initialized when an object is created?

删除回忆录丶 提交于 2019-12-02 11:58:35
I'm writing a hash class: struct hashmap { void insert(const char* key, const char* value); char* search(const char* key); private: unsigned int hash(const char* s); hashnode* table_[SIZE]; // <-- }; As insert() need to check if table[i] is empty when inserting a new pair, so I need all pointers in the table set to NULL at start up. My question is, will this pointer array table_ be automatically initialized to zero, or I should manually use a loop to set the array to zero in the constructor? The table_ array will be uninitialized in your current design, just like if you say int n; . However,

How to sort and rank a vector in C++ (without using C++11)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 03:07:01
问题 I am trying to construct a function take takes a vector, ranks it, sorts it and outputs the sorted and ranked vector with the original positioning of the values. For example: Input: [10,332,42,0.9,0] Output: [3, 5, 4, 2, 1] I used this stack overflow question (specifically Marius' answer) as a reference guide, however I am stuck with my code now and do not understand where the issue is. I am running a C++03. One of the errors I get is error: invalid types ‘const float*[float]’ for array

reinterpret_cast vector of derived class to vector of base class

六月ゝ 毕业季﹏ 提交于 2019-12-02 01:46:50
I have a 3rd-party class, say, class A , and a function accepting vector of class A from the same 3rd-party, say f3() (See simplified program below). For easier use of A , I created a derived class B . Many part of my program used class B . The question is, how can I call f3() with a vector of B as its argument? Is a forced casting in the argument of f3() like the program below a good practice? #include <vector> #include <iostream> #include <algorithm> using namespace std; // a 3rd-party class class A { public: int n; void f1(); }; // my class class B: public A { public: void f2(); }; // a 3rd

How to guard move constructors for C++03 and C++11?

喜欢而已 提交于 2019-12-01 19:39:17
This is similar to What differences, if any, between C++03 and C++11 can be detected at run-time? . But in this case, I want detection to occur via the preprocessor. How should we guard the move constructor (and move assignment ) when the sources are used in both C++03 and C++11? Is the following sufficient (is move semantics something all C++ compilers adopted due to it being essential/core feature)? #if (__cpluplus >= 201103L) Foo(Foo&& other); #endif Or do I need to get into compiler specifics? If we need compiler specific macros, then how do we handle situations like Visual Studio 2012 _

Can friend class be declared conditionally in C++03?

元气小坏坏 提交于 2019-12-01 18:36:41
I want to declare a friend class only if some (compile-time) condition is true. For example: // pseudo-C++ class Foo { if(some_compile_time_condition) { friend class Bar; } }; I did not find any solution on the internet. I went through all the answers to the question Generating Structures dynamically at compile time . Many of them use the C++11 std::conditional , but I would like to know if it is possible to do this in C++03 without using the preprocessor . This solution https://stackoverflow.com/a/11376710/252576 will not work because friend ship is not inherited ( friend class with

How to create a std::map of constant values which is still accessible by the [] operator?

本秂侑毒 提交于 2019-12-01 17:37:59
I need a std:map data structure that is read only, which means I have to fill it once with data and then only read those values, never change them or add additional ones. My non-const version looks like this: //in .h #include <string> #include <map> std::map<std::string, int> myMap; void initMap(); //in .cpp #include "foo.h" void initMap() { myMap["Keys"] = 42; } Then I'd call initMap() once in my code and be done. Now I've read several questions here already and it seems non-trivial to achieve const-ness for the map. Making it a std::map<std::string, const int> won't allow me to fill it in