Strange code breaks are built in MSVC. What does it mean?

I am trying to include rapidxml in my current project. However, it will not be built.

Visual Studio will complain about this piece of code (rapidxml.hpp: 419 + 451):

419: void *memory = allocate_aligned(sizeof(xml_attribute<Ch>)); 420: xml_attribute<Ch> *attribute = new(memory) xml_attribute<Ch>; 

The compiler will say

rapidxml.hpp (420): Error C2061: Syntax error: identifier 'memory'

And I see how this confuses the compiler. It scares me too. What does the (memory) part of the new(memory) xml_attribute<Ch> do there?

If I delete this part (memory) , it just compiles.
In addition, gcc compiles it just fine with (memory) enabled.

Edit:
Oh, and I overloaded new with DEBUG_NEW to do some memory debugging. DEBUG_NEW does not support posting new.

+4
source share
4 answers

This is called "posting new." It creates an xml_attribute instance in memory instead of allocating new memory for it. Cm:

http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10

I'm not sure why VC2010 has a syntax problem.

+1
source

This is my suggestion. The “memory" itself is defined somewhere just like a macro and gets the extension that causes the problem. So search #define.memory (using regular expressions) to determine if memory is defined as a macro.

Regarding the following statement, this form:

 new(allocator) ObjectType(...) 

used when you want to use your own memory allocator to allocate memory for you.

Hope this helps.

+3
source

Do you have #include <new> in this file?

+3
source

The syntax new (pointer) type( argument ) is called the location new and is basically a call to the type constructor with the given argument from the memory previously allocated to pointer .

The syntax, as shown, is correct. Perhaps a couple of lines above there is a missing half-column or a syntax error that confuses the parser, but memory is used as an identifier in accordance with the previous line. (And it is not reserved for language for implementation)

+1
source

Source: https://habr.com/ru/post/1336428/


All Articles