You could select an overload by ranking of conversions, using a dispatching technique:
#include <memory>
#include <iostream>
template<typename T, typename... Args>
auto make_unique_impl(int, Args&&... args)
-> decltype(new T {std::forward<Args>(args)...}, std::unique_ptr<T>{}) {
std::cout << "{..} variant" << std::endl;
return std::unique_ptr<T>(new T { std::forward<Args>(args)... });
}
template<typename T, typename... Args>
auto make_unique_impl(short, Args&&... args)
-> decltype(new T (std::forward<Args>(args)...), std::unique_ptr<T>{}) {
std::cout << "(..) variant" << std::endl;
return std::unique_ptr<T>(new T ( std::forward<Args>(args)... ));
}
// dispatcher
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return make_unique_impl<T>(0, std::forward<Args>(args)...);
}
The call in the dispatcher will prefer the int
overload, since 0
is of type int
. But if substitution fails, the other overload is also viable (via an integral conversion).
Usage example:
struct my_type
{
my_type(int, int) {}
my_type(std::initializer_list<int>) = delete;
};
struct my_other_type
{
my_other_type(int, int) {}
};
int main()
{
make_unique<my_type>(1, 2);
make_unique<my_other_type>(1, 2);
}