c++03

C++03 moving a vector into a class member through constructor (move semantics)

那年仲夏 提交于 2019-12-04 04:31:11
I only have access to C++03 and I often want to move a vector into a function the way you can do it in C++11. The question how to do it not to confuse the user of the code too much. So my question is how did programmers do it before C++11. I know that vector can be "moved" using swap function. So here is what I have come up with: class Foo { public: Foo(std::vector<int>& vec) { using std::swap; swap(vec, m_vec); // "move" vec into member vector } private: std::vector<int> m_vec; }; // usage: std::vector<int> v(100, 1337); Foo foo(v); // v.empty() == true The problem with this approach is that

Getting the type of a member

江枫思渺然 提交于 2019-12-04 01:00:08
NOTE : This question was originally asked way back in 2012. Before the decltype specifier was fully implemented by any major compilers. You should not be looking at this code unless you only have access to C++03. All major C++11 compliant compilers now support decltype . Is there an easy way to retrieve the type of a member? In C++03 struct Person { std::string name; int age; double salary; }; int main() { std::vector<Person> people; // get a vector of people. std::vector<GET_TYPE_OF(Person::age)> ages; ages.push_back(people[0].age); ages.push_back(people[10].age); ages.push_back(people[13]

Does *&++i cause undefined behaviour in C++03?

我与影子孤独终老i 提交于 2019-12-03 23:55:24
In another answer it was stated that prior to C++11, where i is an int , then use of the expression: *&++i caused undefined behaviour. Is this true? On the other answer there was a little discussion in comments but it seems unconvincing. It makes little sense to ask whether *&++i in itself has UB. The deferencing doesn't necessarily access the stored value (prior or new) of i , as you can see by using this as an initializer expression for a reference. Only if an rvalue conversion is involved (usage in such context) is there any question to discuss at all. And then, since we can use the value

Function return type deduction in C++03

廉价感情. 提交于 2019-12-03 20:12:31
The tags ask the question, but nonetheless, consider the following: template<typename F, typename A, typename R> R call(F function, A arg) { return function(arg); } int foo(char) { return 5; } int main() { call(foo, 'a'); } The compiler happily compiles this if parameter R is removed and int is inserted manually as the return type. As shown, the compiler has no way of knowing what to make of R. How can I deduce function return types in C++03? I'm looking for methods that do not require manually specifying the return type and do not require intrusive changes to the other parameters. If this is

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

▼魔方 西西 提交于 2019-12-03 16:31:35
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 here, or how do I get around this problem? I tried searching on StackOverflow to see if this has been

Better way to say x == Foo::A || x == Foo::B || x == Foo::C || …?

别等时光非礼了梦想. 提交于 2019-12-03 16:18:51
问题 Let's say I have a bunch of well-known values, like this (but const char * is just an example, it could be more complicated): const char *A = "A", *B = "B", *C = "C", *D = "D", *E = "E", *F = "F", *G = "G"; Now let's say I want to behave in a particular way if the result of some expression is in a subset of those: if (some_complicated_expression_with_ugly_return_type == A || some_complicated_expression_with_ugly_return_type == C || some_complicated_expression_with_ugly_return_type == E ||

Is there a C++11 to C++03 converter? [closed]

為{幸葍}努か 提交于 2019-12-03 13:38:26
Is there such a tool that is able to convert a code that uses some C++11 features to C++03 compatible code (perhaps using some third party libraries like Boost)? Because nobody has answered with an actual answer, the answer is "No". Just upgrade your toolchain. There are a lot of good reasons to do that anyway. For Ubuntu and other Linux variants, you can even download the package in source form and compile it for your ancient system if you want to use a new toolchain on a very old system. Of course, if you then ship programs with this you will have to link libstdc++ statically or ship the

Do I need a memory barrier for a change notification flag between threads?

好久不见. 提交于 2019-12-03 13:23:29
问题 I need a very fast (in the sense "low cost for reader", not "low latency") change notification mechanism between threads in order to update a read cache: The situation Thread W (Writer) updates a data structure ( S ) (in my case a setting in a map) only once in a while. Thread R (Reader) maintains a cache of S and does read this very frequently. When Thread W updates S Thread R needs to be notified of the update in reasonable time (10-100ms). Architecture is ARM, x86 and x86_64. I need to

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

筅森魡賤 提交于 2019-12-03 13:14:12
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.cpp In file included from main.cpp:1: In file included from /usr/local/bin/../lib/gcc/x86_64-unknown

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

巧了我就是萌 提交于 2019-12-03 10:55:34
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 way we get from that to initializing a standard container is typically through an initializer_list .