c++03

How do I template overload an operator for a group of related classes without conflicting with standard library operators?

痴心易碎 提交于 2019-12-07 12:31:45
问题 This seems like a fairly straight forward idea: I have a group of classes for which I should be able to write an operator, let's say subtraction, using basically the exact same code. When trying to do this the "obvious" way, i.e: template <typename T> T operator-(T a, const T& b) { return a -= b; } Then, on certain compilers, this seems to conflict with a subtraction operator for iterators (specifically it breaks on gcc 3.4.6 and Apple LLVM, while seemingly working fine on gcc version 4 or

Passing a member function to for_each in C++03 (no boost, no c++11)

巧了我就是萌 提交于 2019-12-07 06:04:24
问题 The "solution" below compiles but it is not what I want. I would like to pass the put member function to for_each and not *this . Using boost is NOT an option. Can this be solved within C++03? #include <algorithm> #include <functional> #include <vector> using namespace std; class Wheel { }; class Car { public: void process(const vector<Wheel>& wheel) { for_each(wheel.begin(), wheel.end(), *this); } void operator()(const Wheel& w) { put(w); } private: void put(const Wheel& w) { } }; int main()

Change or check the openmode of a std::ofstream

梦想与她 提交于 2019-12-07 04:34:03
问题 In some code that does a lot of file i/o using std::ofstream , I'm caching the stream for efficiency. However, sometimes I need to change the openmode of the file (e.g. append vs truncate). Here is some similar mock code: class Logger { public: void write(const std::string& str, std::ios_base::openmode mode) { if (!myStream.is_open) myStream.open(path.c_str(), mode); /* Want: if (myStream.mode != mode) { myStream.close(); myStream.open(path.c_str(), mode); } */ myStream << str; } private: std

RAII object for restoring previous value

跟風遠走 提交于 2019-12-07 03:15:00
问题 Perhaps I'm over thinking this but consider the following example: bool some_state = false; // ... later ... some_state = true; do_something(); some_state = false; Now imagine that do_something() can throw. We won't set some_state back to false . What would be nice is to have some sort of automatic stack that pushes/pops based on scope for remembering previous values: { scoped_restore res( some_state, true ); // This sets some_state to true and remembers previous value (false) do_something();

How to define nullptr for supporting both C++03 and C++11? [duplicate]

假装没事ソ 提交于 2019-12-07 00:41:06
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: “Backporting” nullptr to C++-pre-C++0x programs How to define nullptr for supporting both C++03 and C++11? Does below code is compiled with both C++03 and C++11 compiles without change the meaning of nullptr in C++11 compiler? #include <cstddef> #if !defined(nullptr) #define nullptr NULL #endif 回答1: In C++11, nullptr is of type nullptr_t . One of its big advantages compared to NULL (or to 0 would say Bjarne

isSet() or operator void*() or explicit opertor bool() or something else?

拜拜、爱过 提交于 2019-12-06 23:53:45
问题 What is the state of the art about functions to check whether a value is set or not ? For example, the below iterator parses cells. Some cells contain a value, other cells are empty. What is the most convenient way? struct iterator { //usage: bool isset() const // if (it.isset()) bool isSet() const // if (it.isSet()) bool empty() const // if (it.empty()) bool is_set() const // if (it.is_set()) bool is_valid() const // if (it.is_valid()) operator void*() const; // if (it) explicit operator

C++ code generation: create a factory for function pointers

不羁的心 提交于 2019-12-06 13:58:55
问题 I have a long and steadily growing list of (non-member) functions and I need to select one of the functions from this list at runtime (based on a command line argument). At the moment I do this using a factory function which takes a string (the name of the function) and returns a pointer to the function. However this means I have to edit the factory function every time I add a new function (which is both annoying and a violation of the DRY principle). I would like to somehow generate the

Share std::fstream or std::stringstream trough std::iostream

只愿长相守 提交于 2019-12-06 09:44:17
I have a function that creates std::stringstream or std::fstream depending on condition, like: // Some class, stringStream_ and fileStream_ are members // obj.Stream() returns std::iostream& if (condition) { stringStream_.str(std::string()); obj->Stream().rdbuf(stringStream.rdbuf()); } else { boost::filesystem::path temp = boost::filesystem::unique_path(); fileStream_.open(temp.native().c_str(), std::ios_base::trunc | std::ios_base::in | std::ios_base::out); obj->Stream().rdbuf(fileStream_.rdbuf()); } and then this obj object is processed in another thread, so at this moment the function above

Random Number Generator: Should it be used as a singleton?

天涯浪子 提交于 2019-12-06 05:26:32
问题 I use random numbers in several places and usually construct a random number generator whenever I need it. Currently I use the Marsaglia Xorshift algorithm seeding it with the current system time. Now I have some doubts about this strategy: If I use several generators the independence (randomness) of the numbers between the generators depends on the seed (same seed same number). Since I use the time (ns) as seed and since this time changes this works but I am wondering whether it would not be

Why does std::setprecision(6) stream more than six digits in fixed-width mode?

删除回忆录丶 提交于 2019-12-06 04:56:43
问题 The output of the following code: #include <limits> #include <iostream> #include <iomanip> #include <limits> #include <string> #include <sstream> using namespace std; inline string lexical_cast(const float arg) { stringstream ss; ss << fixed << setprecision(numeric_limits<float>::digits10) << arg; if (!ss) throw "Conversion failed"; return ss.str(); } int main() { cout << numeric_limits<float>::digits10 << '\n'; cout << lexical_cast(32.123456789) << '\n'; } is: 6 32.123455 I expected, and