V
V
Vladimir Semenyakin2015-10-18 11:47:25
C++ / C#
Vladimir Semenyakin, 2015-10-18 11:47:25

Do I need to optimize the use of temporary wrappers?

To make it easier to work with the code, I wrote a small class that encapsulates the buffer and its size (something like NSData in Cocoa). Everywhere where it is necessary to work with the data buffer I use this class.
The problem is that there are places in the code where I directly (usually from a third-party API) get the buffer and its size, and I need to pass this data to a function that accepts objects of my buffer class. Now for these purposes I create a temporary variable that wraps the data. I don't like this approach - using temporary objects at least requires calling an extra destructor, which is not really necessary in my case. Making method sets for transferring encapsulated data directly is also not a good idea.
I heard that there is an approach in which we create global objects that serve to transfer the wrapped data. The question is: is this approach really used, and if so, how to implement it as correctly as possible in C++?

How do I see this solution now?

Тупое решение в лоб:
template< typename T_ObjectType >
T_ObjectType &registerObject() {
static T_ObjectType sRegister;
return sRegister;
}
Тут будет огромное количество проблем. Минимум - это ужасно с точки зрения потокобезопасности.

PS: The move semantics, as I understand it, is also not suitable. First, it does not cancel the destructor call. Secondly, I try to write without using C++11 - to be able to use the library on older compilers.
Thanks in advance everyone for your response!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2015-10-18
@semenyakinVS

requires calling an extra destructor
Is it so scary and hits performance hard in your case? In the case of a move, the destructor of the moved object generally doesn't have to do anything.
It is also possible to pass an unnamed temporary object to a function and enable optimizations: copy elision should occur

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question