I have browsed similar threads, but none have answered my question. I am looking for a way to ensure correct overload resolution with two competing constructors, where one takes two parameters and the other one is a variadic template constructor. Take this minimal code example:
template<typename Type>
class MyContainer<Type>
{
public:
using value_type = Type;
using size_type = std::size_t;
using const_reference = const value_type&;
MyContainer(size_type s = 0)
: containerSize(s)
{}
MyContainer(size_type s, const_reference v)
: A(s)
{
// Assign "s" identical values "v" in the container
}
template<typename... Args>
MyContainer(value_type&& v, Args&&... args)
:A()
{
// Assign each argument as an element of the container
}
private:
size_type containerSize;
}
The problem I am facing is that calling the class constructor without explicitly stating the first parameter's type beforehand, it always calls the variadic template constructor. Here is what happens during code execution:
// This calls the variadic template constructor (how to avoid it?)
MyContainer<int> container(10, 55);
// This calls the variadic template constructor (correct, desired)
MyContainer<int> container(1, 2, 3, 4, 5);
// This calls the "count-copies of value" constructor
MyContainer<int>::size_type containerSize = 10;
MyContainer<int> container(containerSize, 55);
Is there a way to reduce the overhead of declaring containerSize before calling the constructor and making sure it does not invoke the variadic template one?