I use a class OneOrMoreIntHelper
that accepts both a single integer as well as an initializer list of integers, and that works great to construct instances of Class
through a single constructor.
However, this fails when trying to use emplace_back
. E.g., this code example does not compile:
#include <initializer_list>
#include <vector>
struct OneOrMoreIntHelper {
OneOrMoreIntHelper(const int value) {}
OneOrMoreIntHelper(const std::initializer_list<int> &) {}
};
struct Class {
Class(OneOrMoreIntHelper) {}
};
void foo() {
Class single_int(0);
Class list_two_ints({0, 0});
std::vector<Class> instances;
// this works fine
instances.emplace_back(0);
// error: no matching function for call to ‘std::vector::emplace_back()’
instances.emplace_back({0, 0});
// Workaround
instances.push_back(OneOrMoreIntHelper{0, 0});
}
I have found the workaround above, but ideally, I would like to not have to instantiate the OneOrMoreIntHelper
object at all (in the end, the whole point is to be able to type 0
instead of {0}
, so having to type OneOrMoreIntHelper{0, 0}
elsewhere complete defeats the purpose).