Are mixes of step-by-step links and bandwidth for the variation pattern function valid?

I have a method that allocates memory for an object, and then calls its constructor, the memory allocator.

template <class T, typename... Arguments> inline T* AllocateObject(Arguments... args) { return new (InternalAllocate(sizeof(T))) T(args...); } 

Can this function be used to mix pass-by-value and pass-by-reference? For example, highlighting a class using a constructor with some reference value and some reference. It compiles, but I'm not sure if it has any unpleasant side effects or not.

+6
source share
1 answer

What you are looking for is a great redirect, i.e. your AllocateObject function should be completely transparent when it comes to copying side effects.

This includes both std::forward (as already mentioned, and nijansen), and the use of generic links in the parameter list:

 template <class T, typename... Arguments> inline T* AllocateObject(Arguments&&... args) // ^^ universal references { return new (InternalAllocate(sizeof(T))) T(std::forward<Arguments>(args)...); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ forwarding } 
+5
source

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


All Articles