language-lawyer

How does C++ free the memory when a constructor throws an exception and a custom new is used

北城以北 提交于 2020-01-01 08:12:04
问题 I see the following constructs: new X will free the memory if X constructor throws. operator new() can be overloaded. The canonical definition of an operator new overload is void *operator new(size_t c, heap h) and the corresponding operator delete . The most common operator new overload is placement new, which is void *operator new(void *p) { return p; } You almost always cannot call delete on the pointer given to placement new . This leads to a single question: How is memory cleaned up when

How does C++ free the memory when a constructor throws an exception and a custom new is used

雨燕双飞 提交于 2020-01-01 08:11:48
问题 I see the following constructs: new X will free the memory if X constructor throws. operator new() can be overloaded. The canonical definition of an operator new overload is void *operator new(size_t c, heap h) and the corresponding operator delete . The most common operator new overload is placement new, which is void *operator new(void *p) { return p; } You almost always cannot call delete on the pointer given to placement new . This leads to a single question: How is memory cleaned up when

How does C++ free the memory when a constructor throws an exception and a custom new is used

纵然是瞬间 提交于 2020-01-01 08:11:09
问题 I see the following constructs: new X will free the memory if X constructor throws. operator new() can be overloaded. The canonical definition of an operator new overload is void *operator new(size_t c, heap h) and the corresponding operator delete . The most common operator new overload is placement new, which is void *operator new(void *p) { return p; } You almost always cannot call delete on the pointer given to placement new . This leads to a single question: How is memory cleaned up when

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

£可爱£侵袭症+ 提交于 2020-01-01 07:31:14
问题 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. 回答1: 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

Adding template specialization in std namespace

蹲街弑〆低调 提交于 2020-01-01 05:48:13
问题 Background: I tried to answer the question Why isn't my overloading < operator not working for STL sort. One of my suggestion (apart from using predicate) was to move the custom operator < for std::string in namespace std so that it can be preferred by the compiler over templated version. At lightening speed the answer was down-voted with following comment from a highly reputed user: This is undefined behaviour, you are not allowed to add declarations to namespace std because it can change

Macro Expansion: Argument with Commas

蹲街弑〆低调 提交于 2020-01-01 05:40:11
问题 The code I'm working on uses some very convoluted macro voodoo in order to generate code, but in the end there is a construct that looks like this #define ARGS 1,2,3 #define MACROFUNC_OUTER(PARAMS) MACROFUNC_INNER(PARAMS) #define MACROFUNC_INNER(A,B,C) A + B + C int a = MACROFUNC_OUTER(ARGS); What is expected is to get int a = 1 + 2 + 3; This works well for the compiler it has originally been written for (GHS) and also for GCC, but MSVC (2008) considers PARAMS as a single preprocessing token

What wording in the C++ standard allows static_cast<non-void-type*>(malloc(N)); to work?

こ雲淡風輕ζ 提交于 2020-01-01 05:11:11
问题 As far as I understand the wording in 5.2.9 Static cast, the only time the result of a void* -to-object-pointer conversion is allowed is when the void* was a result of the inverse conversion in the first place. Throughout the standard there is a bunch of references to the representation of a pointer, and the representation of a void pointer being the same as that of a char pointer, and so on, but it never seems to explicitly say that casting an arbitrary void pointer yields a pointer to the

Can we use va_arg with unions?

≡放荡痞女 提交于 2020-01-01 04:52:08
问题 6.7.2.1 paragraph 14 of my draft of the C99 standard has this to say about unions and pointers (emphasis, as always, added): The size of a union is sufficient to contain the largest of its members. The value of at most one of the members can be stored in a union object at any time. A pointer to a union object, suitably converted, points to each of its members (or if a member is a bit- field, then to the unit in which it resides), and vice versa. All well and good, that means that it is legal

Template Argument Deduction Failure and Function Parameters/Arguments Mismatch

左心房为你撑大大i 提交于 2020-01-01 04:45:12
问题 Consider the following program: template <class T> struct A { using X = typename T::X; }; template <class T, typename A<T>::X* = nullptr> void f(T, int); void f(...); template <class T> void g(T, int, typename A<T>::X* = nullptr); // # void g(...); int main() { // f(0, nullptr); // error g(0, nullptr); // ok } g(0, nullptr) compiles while f(0, nullptr) does not (tested under GCC trunk and Clang trunk on Godbolt). It seems that during the template argument deduction process of # , the compiler

[[maybe_unused]] on enumerator

老子叫甜甜 提交于 2020-01-01 04:37:10
问题 Looking at the specification of the [[maybe_unused]], it states: Appears in the declaration of a class, a typedef­, a variable, a non­static data member, a function, an enumeration, or an enumerator. If the compiler issues warnings on unused entities, that warning is suppressed for any entity declared maybe_unused. As this mentions enumerator, I kinda expect it to have a use-case. As the only thing I could come up with is the -Wswitch warning, I tried it with Clang, GCC and MSVC. enum A { B,