I intend to use shared_ptr
quite a bit in an upcoming project, so (not being aware of std::make_shared
) I wanted to write a variadic template function spnew<T>(...)
as a shared_ptr
-returning stand-in for new
. Everything went smoothly till I attempted to make use of a type whose constructor includes an initializer_list
. I get the following from GCC 4.5.2 when I try to compile the minimal example below:
In function 'int main(int, char**)': too many arguments to function 'std::shared_ptr spnew(Args ...) [with T = Example, Args = {}]' In function 'std::shared_ptr spnew(Args ...) [with T = Example, Args = {}]': no matching function for call to 'Example::Example()'
Oddly enough, I get equivalent errors if I substitute std::make_shared
for spnew
. In either case, it seems to be incorrectly deducing the parameters when an initializer_list
is involved, erroneously treating Args...
as empty. Here's the example:
#include <memory>
#include <string>
#include <vector>
struct Example {
// This constructor plays nice.
Example(const char* t, const char* c) :
title(t), contents(1, c) {}
// This one does not.
Example(const char* t, std::initializer_list<const char*> c) :
title(t), contents(c.begin(), c.end()) {}
std::string title;
std::vector<std::string> contents;
};
// This ought to be trivial.
template<class T, class... Args>
std::shared_ptr<T> spnew(Args... args) {
return std::shared_ptr<T>(new T(args...));
}
// And here are the test cases, which don't interfere with one another.
int main(int argc, char** argv) {
auto succeeds = spnew<Example>("foo", "bar");
auto fails = spnew<Example>("foo", {"bar"});
}
Is this just an oversight on my part, or a bug?