Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 循环模板类型_C++_C++11_Templates_C++14 - Fatal编程技术网

C++ 循环模板类型

C++ 循环模板类型,c++,c++11,templates,c++14,C++,C++11,Templates,C++14,我有一个带有两个模板参数的函数,一个用于向量数据类型(int,float,double,等等),一个用于整数类型(int,int16\u t,uint32\u t,等等): 模板 无效的 添加(std::vector数组,std::vector idx){ // ... } 对于测试,我现在想循环所有可能的数据/整数类型组合,例如 // pseudo code for I in [int, int16_t, int32_t, ...] { for T in [float, double

我有一个带有两个模板参数的函数,一个用于向量数据类型(
int
float
double
,等等),一个用于整数类型(
int
int16\u t
uint32\u t
,等等):

模板
无效的
添加(std::vector数组,std::vector idx){
// ...
}
对于测试,我现在想循环所有可能的数据/整数类型组合,例如

// pseudo code
for I in [int, int16_t, int32_t, ...] {
    for T in [float, double, int, int16_t, ...] {
        // create arguments a, b
        add<T, I>(a, b);
    }
}
//伪代码
对于I in[int,int 16,int 32,…]{
对于T in[float,double,int,int16_T,…]{
//创建参数a、b
添加(a,b);
}
}

是否可以在所有类型上循环?如何做?

可能有更简单的方法,但我会使用,如下所示:

#include <boost/hana/for_each.hpp>
#include <boost/hana/tuple.hpp>
#include <vector>
#include <iostream>
#include <boost/type_index.hpp>

namespace hana = boost::hana;

// for example's sake, just print the type
template <typename T, typename I>
void add(std::vector<T> array, std::vector<I> idx) {

    using namespace boost::typeindex;
    std::cout << type_id<T>().pretty_name() << " - " << type_id<I>().pretty_name() << std::endl;
}

int main() {

    auto types1 = hana::tuple_t<int, int16_t, int32_t>;
    auto types2 = hana::tuple_t<float, double, int, int16_t>;

    hana::for_each(types1, [types2](auto t1) {

        hana::for_each(types2, [t1](auto t2) {

            using t1_type = typename decltype(t1)::type;
            using t2_type = typename decltype(t2)::type;

            add<t1_type, t2_type>({}, {});
        });
    });
}
#包括
#包括
#包括
#包括
#包括
名称空间hana=boost::hana;
//例如,只需打印类型
模板
void add(std::vector数组,std::vector idx){
使用名称空间boost::typeindex;

std::cout如果您手头有boost,那么肯定可以使用
boost::hana
。但是,如果您必须:

#include <iostream>

template<typename... T>
struct iterator {
  template<typename CB_T>
  static void iterate(CB_T const& ) {}
};

template<typename first, typename... rest>
struct iterator<first, rest...> {
  template<typename CB_T>
  static void iterate(CB_T const& cb) {
    cb(first());
    iterator<rest...>::iterate(cb);
  }
};

int main() {
  iterator<int, float>::iterate([](auto const & v_1){
    using v_1_t = decltype(v_1);
    iterator<char, double>::iterate([&](auto const & v_2){
      using v_2_t = decltype(v_2);
      std::cout << typeid(v_1_t).name() << " vs " << typeid(v_2_t).name() << "\n";
    });
  });
  return 0;
}
#包括
模板
结构迭代器{
模板
静态void迭代(CB_T const&){
};
模板
结构迭代器{
模板
静态无效迭代(CB_T const&CB){
cb(first());
迭代器::迭代器(cb);
}
};
int main(){
迭代器::迭代器([](自动常量&v_1){
使用v_1_t=decltype(v_1);
迭代器::迭代器([&](自动常量&v_2){
使用v_2_t=decltype(v_2);

std::你可以使用可变模板吗..谢谢@KerrekSB的提示。但是,我看不到你发布的链接和问题之间的联系。(另外,链接中的代码似乎需要C++17;我仅限于C++14。)基本上是Kerrek SB发布的内容,但在c++14中会起作用。折叠表达式是他语言中唯一一个c++17的部分。通常当问到这样的问题时,它实际上是一个XY问题:OP想要实现某个目标并想出一个解决方案。然后他/她问如何实现他/她的解决方案。也许有更好的解决方案,但你不能ed告诉我们您最终想要实现什么。一般提示可能是类型列表。
#include <iostream>

template<typename... T>
struct iterator {
  template<typename CB_T>
  static void iterate(CB_T const& ) {}
};

template<typename first, typename... rest>
struct iterator<first, rest...> {
  template<typename CB_T>
  static void iterate(CB_T const& cb) {
    cb(first());
    iterator<rest...>::iterate(cb);
  }
};

int main() {
  iterator<int, float>::iterate([](auto const & v_1){
    using v_1_t = decltype(v_1);
    iterator<char, double>::iterate([&](auto const & v_2){
      using v_2_t = decltype(v_2);
      std::cout << typeid(v_1_t).name() << " vs " << typeid(v_2_t).name() << "\n";
    });
  });
  return 0;
}