Jump to content

  • Log In with Google      Sign In   
  • Create Account

King Mir

Member Since 11 Jun 2006
Offline Last Active Jul 02 2016 12:11 AM

Posts I've Made

In Topic: Is there a way to change initialisation order while keeping object layout ?

24 May 2016 - 12:11 AM

You could use one member unions to force class members not to be implicitly initialized in the constructor. Then in the constructor body you can use placement new to initialize that member late. Then, if member field is not trivially copyable, you also need to provide a copy constructor, destructor, and assignment operator.


It's ugly, so only use this technique for measurable performance gain.

In Topic: Non-trivial union

14 May 2016 - 09:26 PM

That's still not correct.

Essentially, if you have a non-trivial type in a union, you have to explicitly call constructors and destructors. Calling mObject in the initializer list does this for object, but then you cannot store an ArrayInfo there, without first calling the destructor. So instead, you should not construct either object in the member initializer expression list, instead using placement new in the constructor body. The code looks like this:

Type(const Type& that) :
    if ( that.mKind == eArray ) {     
      new (&mArray) ArrayInfo{that.mArray}; 
    else {
      new (&mObject) ObjectInfo{that.mObject}; 

(I also made this follow normal copy constructor semantics, which your code didn't do)


Likewise, you need to define a destructor, that will destroy the correct union member explicitly.

In Topic: Is inheritance evil?

15 April 2016 - 08:13 PM

It is good advice to prefer interfaces over inheritance, but there are uses for inheritance too. Inheritance does get over used, probably because it is often taught too early to new programmers.


Personally, I reserve the word "evil" for things that are anti-patterns in all cases. For example, in C++, NULL is evil and can get you into trouble; prefer nullptr in c++11 and later or 0 in C++03 and earlier.

In Topic: [VS2015] Natvis: inspect vector data in custom visualiser

15 April 2016 - 07:52 PM

Your getting into implementation details of std::vector by using _Myfirst. That kind of code isn't portable. Why can't you just call m_vComponents.data()? as in:

<Item Name="Components">m_vComponents.data()</Item>

Of course I'm not familiar with how natvis works, and there are peculiarities related to it.

In Topic: Template-related compiler bug or... ?

15 April 2016 - 12:42 AM

Have you heard about any good progress on concepts?

There's a TS. It will not be in C++17, but possibly C++20.