c++03

How to break shared_ptr cyclic reference using weak_ptr

丶灬走出姿态 提交于 2019-11-28 06:31:48
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 with the following : struct A { boost::weak_ptr<A> wk_ptr; }; boost::shared_ptr<A> ptr_A (boost::make

“Constant expressions” prior to C++11

浪尽此生 提交于 2019-11-27 23:34:11
问题 The constexpr keyword was introduced in C++11, as (I think) was the corresponding idea of "constant expressions." However, this concept was implicitly present in C++98/c++03, since array declarations require a constant expression: // valid: int a[sizeof(int)]; int b[3+7]; int c[13/4]; const int n = 3; int d[n]; // invalid: int m = 4; int e[m]; There are other "constant expressions", i.e., expressions that can be (and/or must be) evaluated at compile-time; one example is template arguments.

Calling delete on NULL pointers - C++03 vs C++11

孤人 提交于 2019-11-27 23:30:20
In the C++03 Standard, I see: 5.3.5 Delete 2 If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. In the first alternative ( delete object ), the value of the operand of delete shall be a pointer to a non-array object or a pointer to a sub-object (1.8) representing a base class of such an object (clause 10). If

Why was the restriction on the comma operator being in a constant expression removed in C++11?

萝らか妹 提交于 2019-11-27 21:59:25
Recently when answering a question I realized that the comma operator is allowed in a constant expression in C++11 as long as the expression is surrounded by () , for example: int a[ (1, 2) ] ; Pre C++11 it is forbidden to use the comma operator in a constant expression, from the draft pre C++11 standard section 5.19 Constant expressions which says ( emphasis mine ): [...]In particular, except in sizeof expressions, functions, class objects, pointers, or references shall not be used, and assignment, increment, decrement, function-call, or comma operators shall not be used. Why was the comma

Default, value and zero initialization mess

只愿长相守 提交于 2019-11-27 16:59:26
I am very confused about value- & default- & zero-initialization. and especially when they kick in for the different standards C++03 and C++11 (and C++14 ). I am quoting and trying to extend a really good answer Value-/Default-/Zero- Init C++98 and C++03 here to make it more general as it would help a lot of users if somebody could help fill out the needed gaps to have a good overview about what happens when? The full insight by examples in a nutshell: Sometimes the memory returned by the new operator will be initialized, and sometimes it won't depending on whether the type you're newing up is

“Backporting” nullptr to C++-pre-C++0x programs

删除回忆录丶 提交于 2019-11-27 16:25:44
问题 More or less what the title suggests. While I'm not yet using C++0x I'd like to be prepared for when it happens, and I'd also like to reduce the amount of code I have to rewrite to use some of its facilities. That way I can get backwards and forwards compatibility in one go. One of the most interesting ones I have found is nullptr, which I've been using more often recently. After checking the "Official workaround" and Meyer's suggestion, I decided that I'd like to use this in both my C++ and

C++03 library with C++11 source code

房东的猫 提交于 2019-11-27 16:19:14
问题 If I have library that was written in C++03 and I compile it to a static library, can I then use it in C++11? Also is the reverse possible ( C++11 static library with C++03 ). Update: The compiler I am using is clang or LLVM 回答1: It depends primarily on how you use the C++ standard library in your library. If you don't use it at all, then you are unlikely to encounter any problems. If you use libstdc++ , then you may encounter some issues: Passing standard library objects to and from your

How to test whether class B is derived from template family of classes

限于喜欢 提交于 2019-11-27 15:17:06
How to test at compile time whether class B is derived from std::vector? template<class A> struct is_derived_from_vector { static const bool value = ????; }; How to test at compile time whether class B is derived from template family? template<class A, template< class > class Family> struct is_derived_from_template { static const bool value = ????; }; Using: template<class T> struct X {}; struct A : X<int> {} struct B : std::vector<char> {} struct D : X<D> {} int main() { std::cout << is_derived_from_template<A, X>::value << std::endl; // true std::cout << is_derived_from_template<D, X>::value

In which versions of the C++ standard does “(i+=10)+=10” have undefined behaviour?

假装没事ソ 提交于 2019-11-27 13:28:48
In C++, does the following have undefined behaviour: int i = 0; (i+=10)+=10; There was some debate about this in the comments to my answer to What's the result of += in C and C++? The subtlety here is that the default response seems to be "yes", whereas it appears that the correct answer is "it depends on the version of the C++ standard". If it does depend on the version of the standard, please explain where it's UB and where it's not. tl;dr : The sequence of the modifications and reads performed in (i+=10)+=10 is well defined in both C++98 and C++11, however in C++98 this is not sufficient to

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

无人久伴 提交于 2019-11-27 10:04:22
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(struct T); } } I am not sure if the examples are 100% correct, but you get the idea. I believe there are