Различные варианты создания таких векторов:
#include <concepts>
#include <type_traits>
#include <vector>
#include <iostream>
template<typename T, typename... Ts>
requires (... && std::is_same_v<Ts, T>)
void foo(T first, Ts... rest) {
std::vector<T> v{first, rest...};
for (const auto& item : v) {
std::cout << item << '\n';
}
}
#include <initializer_list>
template<typename T>
void bar(std::initializer_list<T> elems) {
std::vector<T> v{std::cbegin(elems), std::cend(elems)};
for (const auto& item : v) {
std::cout << item << '\n';
}
}
#include <cstddef>
#include <tuple>
template<std::size_t i, typename... Ts>
using ith_type = std::tuple_element_t<i, std::tuple<Ts...>>;
template<typename... T>
void baz(T... elems) {
std::vector<ith_type<0, T...>> v{elems...};
for (const auto& item : v) {
std::cout << item << '\n';
}
}
int main() {
foo(1, 2, 3);
foo('x', 'y');
// foo(1, 'x');
// foo();
bar({1, 2, 3});
// bar({1, 'x'});
// bar({});
baz(1, 2, 3);
baz(1, 'x'); // 2 тип приводим к 1
// baz();
}
Самый короткий вариант, предложенный участником @user7860670 в комментариях:
#include <vector>
#include <iostream>
template<typename... T>
void foo(T... elems) {
std::vector v{elems...};
for (const auto& item : v) {
std::cout << item << '\n';
}
}
int main() {
foo(1, 2, 3);
foo('x', 'y');
// foo(1, 'x');
// foo();
}