News for hackers. Stuff that matters.
Re:C++ 11 Approved by Jach on Saturday August 13, 2011 @04:34AM (#2880627)
Are you saying there's no way to make it optional and, when it's not being used, maintain the same runtime speeds? Virtual functions are "costly" too, but C++ has them and it's considered poor form to not declare your destructor virtual.
Re:C++ 11 Approved by tomjen3 on Saturday August 13, 2011 @06:16AM (#2880714)
Well the issue is that virtual functions are only costly when they are used an it can (trivially) be determined at compile whether they are in use or not.

On the other hand, you cannot determine if reflection will be used (not, at least without solving the halting problem) and so you have to generate the reflection code for everything.

Re:C++ 11 Approved by Game_Ender on Saturday August 13, 2011 @12:52PM (#2881358)
And what is costly about the reflection code exactly? It is just some static structures with type information and function/data pointers. I fail to see how it can impact performance in anyway. The only side effect will be an increase in binary size.
Re:C++ 11 Approved by pmarin on Saturday August 13, 2011 @05:02AM (#2880644)
sincerely I don't know if is comparable the virtual function's overhead with the reflexion's overhead.

Stroustrup answered a question about reflexion in this interview[1]:

Have you ever considered adding reflection to C++?

BS: Often. However, I have not seen an approach to full reflection directly supported by a language that didn't cause serious overheads. Also, reflection seems to encourage styles of programming that make it hard to determine what's going on from the source text and discourage static checking. I see that as a problem. Consequently, the C++ RTTI provides only the minimal information to determine the types of objects at run time. Where necessary, this can be used as a handle for more information about types (classes), but any such information is beyond what the standard guarantees[2].

[1] http://www2.research.att.com/~bs/omo_interview.html

[2] I think he was talking about "type_info"

Re:C++ 11 Approved by malkia on Saturday August 13, 2011 @06:18AM (#2880716)
MSVC Runtime already keeps names of the classes in the executable (browse the EXE and you would see it).

Really the only loss there would be memory usage. And this can be all optional - where you can waste some memory, for more information about the code currently running - the better.

There's already been such scarifies in C++ - exceptions, RTTI cannot be used everywhere - a xbox console would not handle C++ exceptions for example, yet you can still write in C++ (someone might argue that it's not C++ if it does not have exceptions)

Re:C++ 11 Approved by copper on Saturday August 13, 2011 @11:51AM (#2881165)
> Virtual functions are "costly" too, but C++ has them and it's considered poor form to not declare your destructor virtual.

It's a bit more subtle than that: a virtual destructor is only required when the class, or its base classes, already have another virtual function (and it's only really needed if another class inherits from it. hmm.) At that point, a virtual destructor doesn't add any more cost over and above the virtual function that's already defined.

Re:C++ 11 Approved by malkia on Saturday August 13, 2011 @02:02PM (#2881526)
But not putting one, using foreign code base might get you in trouble (resource leaking, not de-initializing certain piece of the parent, threads not closing, etc.).

This would've been prevented if destructors were always virtual.

My take is that if the compiler cannot make the decision, but it's left to you, in this case it's better to always have the virtual destructors ON.

The problem is more visual than analytical - you would have to dig through the whole hierarchy in several files to understand whether you need to put a virtual destructor or not. Compiler/Linker/some post-proc tool should be better at that job. It already has all that information.

Re:C++ 11 Approved by copper on Sunday August 14, 2011 @12:17AM (#2882792)
Hm, I'd rather pay the cost of having to add a virtual keyword in a few places (easily found using the equivalent of g++ -Wnon-virtual-dtor, and you do ship C++ code that is warning-free, right? :) rather than have to deal with up to eight bytes extra per instance in all my classes.
Re:C++ 11 Approved by malkia on Sunday August 14, 2011 @05:06AM (#2883196)
Thanks for the hint!

Actually I did not know about this option. I've just fixed such bug in one of our tools, written using wxWidgets for Windows. Have to check MSVC on this option.

At the same time a coworker was able to get the Xbox360 analyze thing and it worked finding suspicious stuff in the runtime code.

But we have only the professional version for PC :(

Re:C++ 11 Approved by DCoder on Sunday August 14, 2011 @06:07AM (#2883293)
Re:C++ 11 Approved by cpeterso on Sunday August 14, 2011 @02:47PM (#2884164)
> a virtual destructor is only required when the class, or its base classes, already have another virtual function

Your derived class only needs a virtual destructor if the base class's destructor is public. If the base class destructor is protected (or private), then no one can call delete on a pointer to a base class.

Re:C++ 11 Approved by copper on Monday August 15, 2011 @10:14AM (#2886963)
This is why I rather enjoy discussing horrible hairy corner cases like this - there's always something I've overlooked :)
Re:C++ 11 Approved by xilun0 on Sunday August 14, 2011 @01:18AM (#2882858)
(little nitpicking note : there are cases where it can be useful to have a virtual descriptor as the only virtual method of a class)
Re:C++ 11 Approved by copper on Sunday August 14, 2011 @12:49PM (#2883938)
I'm interested in knowing what these are - barring an alignment-related weirdness, I can't think of any reason to do that.
Re:C++ 11 Approved by xilun0 on Wednesday August 17, 2011 @11:08PM (#2897807)
If you do everything in the constructor and the destructor, and in the meantime store a generic instance (or maybe a list of generic instances) somewhere.

This can be a quite rare situation, though. But not impossible.