example3_factory_functions.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <memory>

class MyClass {
public:
    MyClass(int x) {
        std::cout << "MyClass constructed with " << x << std::endl;
    }
};

template<typename T, typename... Args>
std::unique_ptr<T> make_unique_custom(Args&&... args) {
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

int main() {
    auto obj = make_unique_custom<MyClass>(42);

    return 0;
}
Back to std_forward