c++ - Qt::make_shared for creating QSharedPtr as std::make_shared for creating std::shared_ptr -


As stated in the Barjini Stroustrup's "A Tour of C ++", and a known C ++ 14 practice As such, someone should avoid the naked code in the new and delete . Provides std :: make_shared and std :: make_unique to create smart pointers to store allocated items in the standard library.

However, it is not possible to use these routines for non-standard smart pointers, such as QT's own memory management model (with parent), but also smart pointer class like Provides QSharedPointer and QPointer (although later it is not actually a brochure indicator).

My question is: Is not it convenient to create Qt analog of std :: make_shared ? To like it, QSharedPtr :

  Namespace Qt {template & lt; Class T, squares ... Args & gt; QSharedPointer & LT; T & gt; Make_shared (erg & amp; ... args) {return QSharedPointer & lt; T & gt; (New t (std :: forward & lt; ergos & gt; (args) ...)); }}   

or to create this, QPointer :

  named place QT {template   

and it can be used like this:

  automatically pCancelButton = Qt :: make_ptr & lt; QPushButton & gt; ("Cancel", this);   

Is there any warning for this approach? Is there any publicly known use of this approach?

UPDATE I assume that Qt :: make_ptr is useful unless QPointer is useful, because it New hides the operator and ensures that new is only for certain things that can be found on QObject . Qt users do too many new s, but in this way we can ensure that the new is used only in the QT context, any debate on this idea?

As strongly as the features of make_shared The right forwarding feature , which is available only after C ++ 11 and introduces universal (forwarding) reference , saying that without correct forwarding Using this function may be disabled, Qt is significantly older than the C ++ standard recently, so it was not available until Qt 5.1 (as you have to do with QT's internal pre-PC

Qt 5.1 already provides its own implementation to create smart pointers .

That stable member function can be used as follows:

  auto ptr = QSharedPointer & lt; QPushButton & gt; :: Create ("Cancel", this);   

But note the details:

Note: This function is available with the C ++ 11 compiler which is correct Supports forwarding an arbitrary number of arguments If the compiler does not support the required C ++ 11 features, then you should use the overload which calls the default constructor. new

new :

  1. This special, forwarding task single system stored object and Both can assign to a memory call and reference counter .

  2. The memory allocation is separated from the context of calling, so when you create another object, avoid the memory leak in case of an exception call a function in the example (Where a compiler is free to select the order in which the argument is evaluated). Consider:

      foo (QSharedPointer & lt; QPushButton & gt; (New QPushButton ("Cancel", this)), MayThrow ());           

    That is, if the compiler first the new QPushButton ("Cancel" ", This) expression, and then calls the Mayrhrow () function before calling the constructor of QSharedPointer , if you enter MayThrow ( ) The function throws an exception, so you can leak memory.

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -