If I have a template that wraps a standard container, it seems I can reasonably easily delegate the initializer_list constructor:
template<typename T>
struct holder {
T t_;
holder() :
t_() {}
holder(std::initializer_list<typename T::value_type> values)
: t_(values) {}
};
So this works nicely with std::vector, for instance.
int main(int argc, char* argv[]) {
holder<std::vector<int>> y{1,2,3};
return EXIT_SUCCESS;
}
But it pretty obviously doesn't work for T as 'int', or any other type that doesn't have a nested value_type typedef. So, I'd like to use some sort of enable_if or similar trick to make the initializer_list constructor not be emitted unless T both defines a nested value_type typedef, and is constructible from std::initializer_list.
I tried the following, but it still doesn't work, because the compiler (clang++ 3.1 in my case), still trips over the invalid T::value_type when T is int:
holder(typename std::enable_if<std::is_constructible<T, std::initializer_list<typename T::value_type>>::value, std::initializer_list<typename T::value_type>>::type values)
: t_(values) {}
Any thoughts on how to express the concept "give this template on T an initializer list constructor over T's value_type, if and only if T has a value_type typedef and is constructible from an initializer_list of T::value_type".
initializer_list
passed by value correct, or should it be passed by some reference type? (&&
?) – Martin Bastd::initializer_list
. – Matthieu M.initializer_list
just contains a pointer and a size, so it is ok to pass it by value. The data itself is not copied. – Bo Persson