如何获取传递给可变参数模板函数的每个容器的元素类型
How to get the element type of each container passed to variadic template function
我有一个可变参数模板函数,我正在向它传递一个 std::vector
序列。对于每个 std::vector
对象,我需要获取其元素的类型,以便对其进行一些操作,例如检索编译时间常量 ex T::SIZE
,或检查其大小 sizeof(T)
。这是我尝试过的两种方法:
template<typename T...>
void foo(std::vector<T>...args) {
auto f = [](auto x){
size_t size = sizeof(T);
int var = T::SIZE;
};
(f(args),...);;
}
template<typename T...>
void foo(T...args) {
auto f = [](auto x){
using U = typename x.value_type;
size_t size = sizeof(U);
int var = U::SIZE;
};
(f(args),...);;
}
使用第二种方法,我尝试抽象出 std::vector
的类型,而是使用 std::vector::value_type
方法获取类型。
然后我传递值:
std::vector<A>a;
std::vector<B>b;
std::vector<C>c;
foo(a, b, c);
使用第一种方法编译器给我:
error: parameter packs not expanded with ‘...’: auto f = ...
使用第二种方法我得到:
error: expected nested-name-specifier before ‘x’ using U = typename x.value_type;
- 模板参数应声明为
typename... T
而不是 typename T...
。
- 您应该从参数
x
中获取类型,然后从中获取 value_type
,例如typename decltype(x)::value_type
.
- 在第一种情况下,您也应该从参数
x
中获取信息,与第二种情况类似。
第一个,
template<typename... T>
void foo(std::vector<T>...args) {
auto f = [](auto x){
using U = typename decltype(x)::value_type;
int size = sizeof(U);
int var = U::VAR;
};
(f(args),...);;
}
第二个:
template<typename... T>
void foo(T...args) {
auto f = [](auto x){
using U = typename decltype(x)::value_type;
int size = sizeof(U);
int var = U::VAR;
};
(f(args),...);;
}
您必须执行以下操作:
template<typename ...T>
void foo(T...args) {
auto f = [](auto x){
using U = typename decltype(x)::value_type;
int size = sizeof(U);
int var = sizeof(U);
};
(f(args),...);;
}
如果稍后您想要更改 lambda 以接受引用,例如 f = [](auto&& x)
,您必须使用 std::decay_t
,因为 decltype(x)
会给您引用类型,并且引用类型没有任何嵌套类型成员:
template<typename ...T>
void foo(T...args) {
auto f = [](auto&& x){
using U = typename std::decay_t<decltype(x)>::value_type;
int size = sizeof(U);
int var = sizeof(U);
};
(f(args),...);;
}
我有一个可变参数模板函数,我正在向它传递一个 std::vector
序列。对于每个 std::vector
对象,我需要获取其元素的类型,以便对其进行一些操作,例如检索编译时间常量 ex T::SIZE
,或检查其大小 sizeof(T)
。这是我尝试过的两种方法:
template<typename T...>
void foo(std::vector<T>...args) {
auto f = [](auto x){
size_t size = sizeof(T);
int var = T::SIZE;
};
(f(args),...);;
}
template<typename T...>
void foo(T...args) {
auto f = [](auto x){
using U = typename x.value_type;
size_t size = sizeof(U);
int var = U::SIZE;
};
(f(args),...);;
}
使用第二种方法,我尝试抽象出 std::vector
的类型,而是使用 std::vector::value_type
方法获取类型。
然后我传递值:
std::vector<A>a;
std::vector<B>b;
std::vector<C>c;
foo(a, b, c);
使用第一种方法编译器给我:
error: parameter packs not expanded with ‘...’: auto f = ...
使用第二种方法我得到:
error: expected nested-name-specifier before ‘x’ using U = typename x.value_type;
- 模板参数应声明为
typename... T
而不是typename T...
。 - 您应该从参数
x
中获取类型,然后从中获取value_type
,例如typename decltype(x)::value_type
. - 在第一种情况下,您也应该从参数
x
中获取信息,与第二种情况类似。
第一个,
template<typename... T>
void foo(std::vector<T>...args) {
auto f = [](auto x){
using U = typename decltype(x)::value_type;
int size = sizeof(U);
int var = U::VAR;
};
(f(args),...);;
}
第二个:
template<typename... T>
void foo(T...args) {
auto f = [](auto x){
using U = typename decltype(x)::value_type;
int size = sizeof(U);
int var = U::VAR;
};
(f(args),...);;
}
您必须执行以下操作:
template<typename ...T>
void foo(T...args) {
auto f = [](auto x){
using U = typename decltype(x)::value_type;
int size = sizeof(U);
int var = sizeof(U);
};
(f(args),...);;
}
如果稍后您想要更改 lambda 以接受引用,例如 f = [](auto&& x)
,您必须使用 std::decay_t
,因为 decltype(x)
会给您引用类型,并且引用类型没有任何嵌套类型成员:
template<typename ...T>
void foo(T...args) {
auto f = [](auto&& x){
using U = typename std::decay_t<decltype(x)>::value_type;
int size = sizeof(U);
int var = sizeof(U);
};
(f(args),...);;
}