A vector
?
template <typename T>
std::vector<T> makearr (T a, T b, T c)
{
T d[] = { a, b, c };
return std::vector<T>(d, d + 3);
}
std::vector<int> x = makearr(1, 2, 3);
// access x[i]
If all manner of standard containers are verboten, then:
template <typename T> struct three { T d[3]; };
template <typename T>
three<T> makearr (T a, T b, T c)
{
three<T> d = { { a, b, c } };
return d;
}
three<int> x = makearr(1, 2, 3);
// access x.d[i]
Borrowing from here and here, I cobbled together this C++11 solution that doesn't use standard containers:
template <typename T, unsigned N>
struct argh {
argh (std::initializer_list<T> i) {
std::copy_n(i.begin(), std::min(i.size(), N), v_);
}
T & operator [] (int i) { return v_[i]; }
const T & operator [] (int i) const { return v_[i]; }
private:
T v_[N];
};
template <typename T, typename... Type>
auto makearr (T&& val, Type&&... vals) -> argh<T, 1 + sizeof...(Type)>
{
argh<T, 1 + sizeof...(Type)> arr = { val, vals... };
return arr;
}
auto x = makearr(1, 2, 3);
// access x[i]