c++03

assigning char to int reference and const int reference in C++

流过昼夜 提交于 2019-11-27 06:45:46
问题 I noticed that assigning a char to a const int& compiles, but assigning it to a int& gives a compilation error. char c; int& x = c; // this fails to compile const int& y = c; // this is ok I understand that it is not a good practice to do this, but I am curious to know the reason why it happens. I have searched for an answer by looking for "assigning to reference of different type", "assigning char to a int reference", and "difference between const reference and non-const reference", and came

Thread safety of C++ std Containers

主宰稳场 提交于 2019-11-27 06:44:57
问题 I read a lot of posts here with the question if the standard containers for C++ (like "list" or "map" are thread safe and all of them said that it is not in general. Parallel reads should be OK, but parallel writes or parallel reads and writes may cause problems. Now I found out that at www.cplusplus.com that accessing or modifying the list during most of the operations is safe. Some examples: map::find The container is accessed (neither the const nor the non-const versions modify the

C++03. Test for rvalue-vs-lvalue at compile-time, not just at runtime

南楼画角 提交于 2019-11-27 05:42:53
问题 In C++03, Boost's Foreach, using this interesting technique, can detect at run-time whether an expression is an lvalue or an rvalue. (I found that via this StackOverflow question: Rvalues in C++03 ) Here's a demo of this working at run-time (This is a more basic question that arose while I was thinking about this other recent question of mine. An answer to this might help us answer that other question.) Now that I've spelled out the question, testing rvalue-ness in C++03 at compile-time, I'll

Do these members have unspecified ordering?

风格不统一 提交于 2019-11-27 04:58:13
A colleague told me that, in the following type, all members have unspecified ordering in memory (relative to one another). I doubt this, because they all have the same access level. Who is correct? struct foo { public: int x; public: int y; public: int z; }; Your colleague is correct for C++03: [C++03: 9.2/12]: Nonstatic data members of a (non-union) class declared without an intervening access-specifier are allocated so that later members have higher addresses within a class object. The order of allocation of nonstatic data members separated by an access-specifier is unspecified (11.1). [..]

What differences, if any, between C++03 and C++11 can be detected at run-time?

不打扰是莪最后的温柔 提交于 2019-11-27 04:01:39
问题 It is possible to write a function, which, when compiled with a C compiler will return 0, and when compiled with a C++ compiler, will return 1 (the trivial sulution with #ifdef __cplusplus is not interesting). For example: int isCPP() { return sizeof(char) == sizeof 'c'; } Of course, the above will work only if sizeof (char) isn't the same as sizeof (int) Another, more portable solution is something like this: int isCPP() { typedef int T; { struct T { int a[2]; }; return sizeof(T) == sizeof

How can I open a file for reading & writing, creating it if it does not exist, without truncating it?

夙愿已清 提交于 2019-11-27 03:54:27
问题 What is the proper set of I/O flags for a std::fstream , where I want to be able to read from and write to the file, without truncating the file if it exists, but creating it if it does not? I've tried std::ios::binary | std::ios::in | std::ios::out std::ios::binary | std::ios::in | std::ios::out | std::ios::ate but neither of these create the file if it does not already exist. I don't want std::ios::app , because I also need to be able to seek around the file at will, with both the get and

What restrictions does ISO C++03 place on structs defined at function scope?

你。 提交于 2019-11-27 03:52:38
问题 We're not allowed to define a functor struct inside a function because one is not allowed to use function declared structs in the instantiation of function templates. Are there any other significant pitfalls to be aware of? E.g. would this be bad: int foo() { struct Scratch { int a, b, c; }; std::vector<Scratch> workingBuffer; //Blah Blah } 回答1: 1. C++ standard forbids using locally-defined classes with templates. 14.3.1/2 : A local type, a type with no linkage, an unnamed type or a type

operator modulo change in c++ 11? [duplicate]

你离开我真会死。 提交于 2019-11-27 03:21:29
问题 Possible Duplicate: C++ operator % guarantees In c++ 98/03 5.6-4 The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined; otherwise (a/b)*b + a%b is equal to a. If both operands are nonnegative then the remainder is nonnegative; if not, the sign of the remainder is implementation-defined . In c++ 11: 5.6 -4 The binary / operator yields

How to break shared_ptr cyclic reference using weak_ptr

佐手、 提交于 2019-11-27 01:23:17
问题 I have read that weak_pointers can be used to break cyclic references. Consider the following example of a cyclic reference struct A { boost::shared_ptr<A> shrd_ptr; }; boost::shared_ptr<A> ptr_A(boost::make_shared<A>()); boost::shared_ptr<A> ptr_b(boost::make_shared<A>()); ptr_A->shrd_ptr = ptr_b; ptr_b->shrd_ptr = ptr_A; Now above is a case of cyclic reference and I wanted to know how I can break the cyclic reference above by using weak_ptr ? Update : Based on suggestion received I came up

Is there any reason to use the 'auto' keyword in C++03?

本秂侑毒 提交于 2019-11-26 21:29:13
Note this question was originally posted in 2009, before C++11 was ratified and before the meaning of the auto keyword was drastically changed. The answers provided pertain only to the C++03 meaning of auto -- that being a storage class specified -- and not the C++11 meaning of auto -- that being automatic type deduction. If you are looking for advice about when to use the C++11 auto , this question is not relevant to that question. For the longest time I thought there was no reason to use the static keyword in C, because variables declared outside of block-scope were implicitly global. Then I