Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variadic Function That Calls Constructor With Given Arguments

Need to create variadic template<T> function with different arg types that will call constructor of T with given arguments, sort of like when creating a thread but reverse (when creating a thread, its constructor calls function at given funct pointer).

So in pseudo code it should look something like this

template<typename T>
T* CreateNew( ARGS ) {
    return new T( ARGS );    //Constructor Gets Same Arguments That Were 
}                            //Passed To The Function

Want this to behave like when creating threads, when they call functions with their arguments that are of different types (which I am unsure how to achive).

like image 889
DeltaJuliet2k Avatar asked Mar 13 '26 12:03

DeltaJuliet2k


1 Answers

Its not entirely clear to me but I think you want to look up variadic templates, e.g.:

template <typename T, typename... Args>
T* CreateNew(Args... args) {
    return new T(args...);
}

Adding Example:

#include <iostream>

class A {
    public:
    A(int a){
        std::cout<<__PRETTY_FUNCTION__<<std::endl;
    }
    A(std::string a){
        std::cout<<__PRETTY_FUNCTION__<<std::endl;
    }
    A(int a,std::string b){
        std::cout<<__PRETTY_FUNCTION__<<std::endl;
    }
};

template<typename T, typename... Args>
T* create(Args... args){
    return new T(args...);
}

int main(){
    A b(1);
    A c("a");
    A d(1,"a");
    A* bp = create<A>(1);
    A* cp = create<A>("a");
    A* dp = create<A>(1,"a");
    // This code leaks
    return 0;   
}

Note that because the changes are kept as minimal as possible, we still return a T* here as the original code. Most of the time, this is not a good idea since ownership is passed via raw pointers. Therefore, as suggested by the comments, you might want to use a std::unique_ptr, which would make your CreateNew function basically equivalent to std::make_unique.

like image 57
tos-1 Avatar answered Mar 15 '26 01:03

tos-1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!