c++03

Union initialization in C++ and C

戏子无情 提交于 2019-12-05 08:44:39
问题 I have built a working C library, that uses constants, in header files defined as typedef struct Y { union { struct bit_field bits; uint8_t raw[4]; } X; } CardInfo; static const CardInfo Y_CONSTANT = { .raw = {0, 0, 0, 0 } }; I know that the .raw initializer is C only syntax. How do I define constants with unions in them in a way such that I can use them in C and C++. 回答1: I had the same problem. For C89 the following is true: With C89-style initializers, structure members must be initialized

RAII object for restoring previous value

好久不见. 提交于 2019-12-05 07:44:32
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(); } // At this point, res is destroyed and sets some_state back to false (previous value) Does boost

Change or check the openmode of a std::ofstream

心已入冬 提交于 2019-12-05 06:40:14
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::ofstream myStream; std::string path = "/foo/bar/baz"; } Does anyone know if: There is a way to change

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

↘锁芯ラ 提交于 2019-12-05 04:08:23
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 bool() const; // if ((bool)it) or if(it) //thanks @stijn operator bool() const; // if (it) //why not

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

僤鯓⒐⒋嵵緔 提交于 2019-12-05 03:14:20
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 In C++11, nullptr is of type nullptr_t . One of its big advantages compared to NULL (or to 0 would say Bjarne since he does not like macros) is that between these two functions: void foo(char*); void foo(int); foo(nullptr) will call the char* overload but foo(NULL) will call the int

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

こ雲淡風輕ζ 提交于 2019-12-04 19:43:06
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . 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)? 回答1: Because nobody has answered with an actual

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

筅森魡賤 提交于 2019-12-04 12:39:59
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 better to use only one singular generator and e.g. to make it available as a singleton. Would this

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

喜你入骨 提交于 2019-12-04 10:05:38
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 wanted: 6 32.1234 because, to the best of my knowledge, that's the extent of what a float can reliably

Overriding new with debug version without damaging placement new

筅森魡賤 提交于 2019-12-04 05:35:25
Microsoft runtime library provides debug version of allocation functions. For C++ this is a debug variant of operator new with the signature: void *operator new(size_t size, int blockType, const char *filename, int linenumber); and a macro is defined like #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) Now to instrument all allocations, one normally defines #if defined DEBUG_NEW #define new DEBUG_NEW #endif However this definition breaks any place that uses placement new, because the two sets of arguments end up being syntax error. Now I can easily handle the few uses in our code, but

Why use = to initialise a primitive type in C++?

不想你离开。 提交于 2019-12-04 05:00:27
Where I work, people mostly think that objects are best initialised using C++-style construction (with parentheses), whereas primitive types should be initialised with the = operator: std::string strFoo( "Foo" ); int nBar = 5; Nobody seems to be able to explain why they prefer things this way, though. I can see that std::string = "Foo"; would be inefficient because it would involve an extra copy, but what's wrong with just banishing the = operator altogether and using parentheses everywhere? Is it a common convention? What's the thinking behind it? Unless you've proven that it matters with