c++03

How to setup a global container (C++03)?

痴心易碎 提交于 2019-12-21 05:45:07
问题 I want to define a global container (C++03), and here's an example code I tried, which does not work. #include <vector> #include <string> using namespace std; vector<string> Aries; Aries.push_back("Taurus"); // line 6 int main() {} Compile error: prog.cpp:6:1: error: 'Aries' does not name a type It seems I can define an empty global vector, but cannot fill it up. Looks like in C++03, I cannot specify an initializer either, such as: vector<string> Aries = { "Taurus" }; Have I made a mistake

I'm trying to nest boost's “map_list_of” in C++03, but apparently construction is ambiguous?

早过忘川 提交于 2019-12-21 04:17:18
问题 Consider this: #include <iostream> #include <map> #include <string> #include <boost/assign/list_of.hpp> using boost::assign::map_list_of; const std::map<int, std::map<int, char> > test = map_list_of (100, map_list_of (1, 'a') (2, 'b') ) (101, map_list_of (1, 'c') (2, 'd') ) ; int main() { std::cout << test.find(101)->second.find(2)->second << "\n"; } I wanted the result to be a program that, when executed, outputs d . Instead, I get this: $ clang++ -std=c++03 -O2 -Wall -pedantic -pthread main

Why does GCC 6.3 compile this Braced-Init-List code without explicit C++11 support?

别来无恙 提交于 2019-12-21 03:52:44
问题 I have a question about the different meanings of a curly-brace enclosed list. I know that C++03 did not support C++11's initializer_list . Yet, even without the -std=c++11 compiler flag, gcc 6.3 will properly initialize interpolate with this code: map<string, string> interpolate = { { "F", "a && b && c" }, { "H", "p ^ 2 + w" }, { "K", "H > 10 || e < 5" }, { "J", "F && !K" } }; I was challenged on why this would work, and I realized I didn't have an answer. This is a Brace-Init-List, but the

reinterpret_cast vector of derived class to vector of base class

风流意气都作罢 提交于 2019-12-20 03:17:14
问题 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

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

删除回忆录丶 提交于 2019-12-19 22:00:12
问题 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

How to move elements out of STL priority queue

﹥>﹥吖頭↗ 提交于 2019-12-19 05:47:58
问题 C++'s STL priority queue have a void pop() method, and a const ref top() method. Thus, if you want to move elements out of the queue, you have to do something like this: T moved = std::move(const_cast<T&>(myQueue.top()))); myQeue.pop(); This effectively casts the top to not a constant, so that it can be moved (rather than copied). I don't like this code, because the forced move may invalidate the invariants of the priority queue, which should not matter because of the pop, but things could go

Vector of structs with const members?

柔情痞子 提交于 2019-12-19 05:24:16
问题 Let's say I have #include <string> #include <vector> using namespace std; struct Student { const string name; int grade; Student(const string &name) : name(name) { } }; How do I, then, keep a vector of students? int main() { vector<Student> v; // error C2582: 'operator =' function is unavailable in 'Student' v.push_back(Student("john")); } Is there even a way to do this, or must I allocate all the students on the heap, and store a pointer to each of them instead? 回答1: You can't. Your type

Type safe enum bit flags

↘锁芯ラ 提交于 2019-12-19 05:17:20
问题 I'm looking to use a set of bit flags for my current issue. These flags are (nicely) defined as part of an enum , however I understand that when you OR two values from an enum the return type of the OR operation has type int . What I'm currently looking for is a solution which will allow the users of the bit mask to remain type safe, as such I have created the following overload for operator | enum ENUM { ONE = 0x01, TWO = 0x02, THREE = 0x04, FOUR = 0x08, FIVE = 0x10, SIX = 0x20 }; ENUM

reinterpret_cast vector of class A to vector of class B

丶灬走出姿态 提交于 2019-12-18 09:47:57
问题 Say I have two classes A and B , and a vector of class A as below: class A { int foo; int bar; void someMethod(); }; class B { uint foo; uint bar; void someOtherMethod(); }; std::vector<A> va; and I want to interpret va as a vector of B, since int and uint are re-interpretable. What is the best practice to do the re-interpretation? For example, if I want to invoke someOtherMethod() on va , I can do ((std::vector<B> *)(&va))->someOtherMethod() . But is it the best practice? It seems to me that

No type named 'unique_ptr' in namespace 'std' when compiling under LLVM/Clang

本小妞迷上赌 提交于 2019-12-18 05:18:12
问题 I'm catching a compile error when attempting to use unique_ptr on Apple platforms with -std=c++11 : $ make c++ -std=c++11 -DNDEBUG -g2 -O3 -fPIC -march=native -Wall -Wextra -pipe -c 3way.cpp In file included ... ./smartptr.h:23:27: error: no type named 'unique_ptr' in namespace 'std' using auto_ptr = std::unique_ptr<T>; ~~~~~^ ./smartptr.h:23:37: error: expected ';' after alias declaration using auto_ptr = std::unique_ptr<T>; According to Marshall Clow, who I consider an expert on the C++