C++ 如何获取传递给可变模板函数的每个容器的元素类型

C++ 如何获取传递给可变模板函数的每个容器的元素类型,c++,templates,variadic-templates,C++,Templates,Variadic Templates,我有一个可变模板函数,我正在向它传递一个序列std::vectors。对于每个std::vector对象,我需要获取其元素的类型,以便对其执行一些操作,例如检索编译时常量exT::SIZE,或者检查其大小sizeof(T)。以下是我尝试过的两种方法: template<typename T...> void foo(std::vector<T>...args) { auto f = [](auto x){ size_t size = size

我有一个可变模板函数,我正在向它传递一个序列
std::vector
s。对于每个
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),...);;
}
对于第一种方法,编译器为我提供:

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\u type
    ,例如
    typename decltype(x)::value\u 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),...);;
    }
    
    模板
    void foo(std::vector…args){
    自动f=[](自动x){
    使用U=typename decltype(x)::value\U 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),...);;
    }
    
    模板
    无效foo(T…args){
    自动f=[](自动x){
    使用U=typename decltype(x)::value\U type;
    int size=sizeof(U);
    int-var=U::var;
    };
    (f(args),…);;
    }
    

  • 模板参数应声明为
    typename。。。T
    而不是
    typename T.
  • 您应该从参数
    x
    中获取类型,然后从中获取
    value\u type
    ,例如
    typename decltype(x)::value\u 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),...);;
    }
    
    模板
    void foo(std::vector…args){
    自动f=[](自动x){
    使用U=typename decltype(x)::value\U 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),...);;
    }
    
    模板
    无效foo(T…args){
    自动f=[](自动x){
    使用U=typename decltype(x)::value\U type;
    int size=sizeof(U);
    int-var=U::var;
    };
    (f(args),…);;
    }
    

    您必须执行以下操作:

    模板
    无效foo(T…args){
    自动f=[](自动x){
    使用U=typename decltype(x)::value\U type;
    int size=sizeof(U);
    int var=sizeof(U);
    };
    (f(args),…);;
    }
    
    如果以后要将lambda更改为接受引用,例如
    f=[](auto&&x)
    ,则必须使用
    std::decation\t
    ,因为
    decltype(x)
    将为您提供引用类型,而引用类型没有任何嵌套的类型成员:

    模板
    无效foo(T…args){
    自动f=[](自动和x){
    使用U=typename std::decage\U t::value\U type;
    int size=sizeof(U);
    int var=sizeof(U);
    };
    (f(args),…);;
    }
    

    示例:

    您必须执行以下操作:

    模板
    无效foo(T…args){
    自动f=[](自动x){
    使用U=typename decltype(x)::value\U type;
    int size=sizeof(U);
    int var=sizeof(U);
    };
    (f(args),…);;
    }
    
    如果以后要将lambda更改为接受引用,例如
    f=[](auto&&x)
    ,则必须使用
    std::decation\t
    ,因为
    decltype(x)
    将为您提供引用类型,而引用类型没有任何嵌套的类型成员:

    模板
    无效foo(T…args){
    自动f=[](自动和x){
    使用U=typename std::decage\U t::value\U type;
    int size=sizeof(U);
    int var=sizeof(U);
    };
    (f(args),…);;
    }
    

    示例:

    我将此标记为答案,因为它包括传递引用时的案例,这非常有用。您能解释一下为什么我们在传递引用时需要使用
    std::decation\u t
    吗?@LennyWhite请参见编辑
    decltype(x)
    提供
    x
    的声明类型。如果此类类型是引用,则必须删除该引用,以便可以访问其嵌套类型。您也可以使用
    remove_reference
    ,但这更难拼写。我明白了,谢谢!出于某种原因,我似乎没有在
    std::decay\u t
    上找到关于
    std::decay
    std::remove\u reference
    的文档。出于好奇,尝试使用最后两个会在“class std::detacy&”中给我
    没有名为“value\u type”的类型。我将此标记为答案,因为它包含了传递引用时的情况,这非常有用。您能解释一下为什么我们在传递引用时需要使用
    std::decation\u t
    吗?@LennyWhite请参见编辑
    decltype(x)
    提供
    x
    的声明类型。如果此类类型是引用,则必须删除该引用,以便可以访问其嵌套类型。您也可以使用
    remove_reference
    ,但这更难拼写。我明白了,谢谢!出于某种原因,我似乎没有在
    std::decay\u t
    上找到关于
    std::decay
    std::remove\u reference
    的文档。只是出于好奇,尝试使用最后两个,在“class std::decay&”中没有名为“value\u type”的类型。