Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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++_Templates_C++11_Variadic Templates_C++14 - Fatal编程技术网

C++ 在编译时推导整个向量空间

C++ 在编译时推导整个向量空间,c++,templates,c++11,variadic-templates,c++14,C++,Templates,C++11,Variadic Templates,C++14,受到这个问题的启发: 我开始怀疑是否有一种形式的模板代码可以从下面的语句中: using X = axis_limits<-10, +10>; using Y = axis_limits<-10, +10>; using Z = axis_limits<-10, +10>; auto space = std::vector<point>{ generate_point_space<X, Y, Z> }; int的单个维度很简单(代码

受到这个问题的启发:

我开始怀疑是否有一种形式的模板代码可以从下面的语句中:

using X = axis_limits<-10, +10>;
using Y = axis_limits<-10, +10>;
using Z = axis_limits<-10, +10>;

auto space = std::vector<point>{ generate_point_space<X, Y, Z> };
int的单个维度很简单(代码如下)。今天我无法理解这个问题的多方面性

#include <utility>
#include <iostream>
#include <vector>

template<int Begin, int End>
struct axis_limits
{
    static constexpr int first = Begin;
    static constexpr int last = End;
};

namespace details
{
    template<typename Int, typename, Int Begin, bool Increasing>
    struct integer_range_impl;

    template<typename Int, Int... N, Int Begin>
    struct integer_range_impl<Int, std::integer_sequence<Int, N...>, Begin, true> {
        using type = std::integer_sequence<Int, N+Begin...>;
    };

    template<typename Int, Int... N, Int Begin>
    struct integer_range_impl<Int, std::integer_sequence<Int, N...>, Begin, false> {
        using type = std::integer_sequence<Int, Begin-N...>;
    };
}

template<typename Int, Int Begin, Int End>
using integer_range = typename details::integer_range_impl<
Int,
std::make_integer_sequence<Int, (Begin<End) ? End-Begin : Begin-End>,
Begin,
(Begin<End) >::type;

template<int...Is>
std::vector<int> make_vector(std::integer_sequence<int, Is...>)
{
    return std::vector<int> { Is... };
}

template<int Begin, int End>
struct axis_range
{
    using sequence_type = integer_range<int, Begin, End>;
    static constexpr int size = sequence_type::size();
    static std::vector<int> as_vector()
    {
        return make_vector(sequence_type {});
    }
};

template< int Begin, int End >
std::vector<int> make_axis(const axis_limits<Begin, End> &)
{
    return axis_range<Begin, End>::as_vector();
}

template<class T>
void dump_vector(std::ostream& os, const std::vector<T>& v) {
    const char* sep = "{ ";
    for(const auto& i : v) {
        os << sep << i;
        sep = ", ";
    }
    os << " }";
}

template<class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    dump_vector(os, vec);
    return os;
}

using namespace std;

int main()
{

    using X = axis_limits<-5, +5>;
    auto space = std::vector<int>(make_axis(X{}));
    cout << space << endl;
    return 0;
}
我要找的是:

{ { -10, -10, -10 }, { -10, -10, -9 } .... { 9, 9, 8 }, { 9, 9, 9 } }
不是
constexpr
,所以你在这里运气不好。不能从工厂函数中以
constexpr
的形式返回它们,向量总是在运行时构造的。也许你应该试试?差不多

#include <iostream>
#include <utility>
#include <array>

template<int...Is>
constexpr auto make_array(const std::integer_sequence<int, Is...>& param)
{
    return std::array<int, sizeof...(Is)> {Is...};
}

int main()
{
    constexpr std::integer_sequence<int, 1,2,3,4> iseq{};
    constexpr auto arr = make_array(iseq);

    for(auto elem: arr)
        std::cout << elem << " ";
}
#包括
#包括
#包括
模板
constexpr自动生成数组(const std::integer_序列和参数)
{
返回std::数组{Is…};
}
int main()
{
constexpr std::integer_序列iseq{};
constexpr auto arr=make_数组(iseq);
用于(自动元素:arr)

std::cout您可以执行以下操作:

template<int Begin, int End>
struct axis_limits
{
    static constexpr int first = Begin;
    static constexpr int last = End;
    static constexpr int range = End - Begin + 1;
};

struct point
{
    explicit point(int x, int y, int z) : x(x), y(y), z(z) {}
    int x; int y; int z;
};

namespace detail
{

    template <typename X, typename Y, typename Z, std::size_t... Is>
    std::vector<point> generate_point_space_impl(std::index_sequence<Is...>)
    {
        return {point(
            static_cast<int>(Is / (Z::range * Y::range)) % X::range + X::first,
            static_cast<int>(Is / Z::range) % Y::range + Y::first,
            static_cast<int>(Is) % Z::range + Z::first)...
            };
    }

}


template <typename X, typename Y, typename Z>
std::vector<point> generate_point_space()
{
    return detail::generate_point_space_impl<X, Y, Z>(std::make_index_sequence<X::range * Y::range * Z::range>());
}
模板
结构轴极限
{
静态constexpr int first=开始;
静态constexpr int last=End;
静态constexpr int range=End-Begin+1;
};
结构点
{
显式点(intx,inty,intz):x(x),y(y),z(z){
整数x;整数y;整数z;
};
名称空间详细信息
{
模板
std::vector generate\u point\u space\u impl(std::index\u序列)
{
返回{点(
静态强制转换(Is/(Z::range*Y::range))%X::range+X::first,
静态强制转换(Is/Z::range)%Y::range+Y::first,
静态_cast(是)%Z::range+Z::first)。。。
};
}
}
模板
std::向量生成_点_空间()
{
返回详细信息::generate_point_space_impl(std::make_index_sequence());
}

一些元编程帮助程序可用于处理类型列表:

template<class T>struct tag{using type=T;};
template<class Tag>using type=typename Tag::type;

template<class...>struct types{using type=types;};

template<class...Ts>
struct cat;
template<class...Ts>
using cat_t=type<cat<Ts...>>;

template<class...As, class...Bs, class...Ts>
struct cat< types<As...>, types<Bs...>, Ts... >:
  cat< types<As...,Bs...>, Ts... >
{};
template<class...Ts>
struct cat< types<Ts...> >:
  types<Ts...>
{};
template<>
struct cat<>:
  types<>
{};
可以使用它轻松地进行交叉积(与
cat\t
mapper\t
一起使用):

模板
结构交叉乘积:类型{};
模板
使用交叉乘积=类型;
模板
结构交叉产品:cat<
制图员<
绑定第一个::模板应用,
交叉积
>...
>{};
现在我们研究下一个问题,我们有一组点,我们想要生成它们的叉积

template<class...Seq>
struct coords;
template<class...Seq>
using coords_t=type<coords<Seq...>>;
template<class T, T...ts, class...Ts>
struct coords< std::integer_sequence<T,ts...>, Ts... >:
  types_to_seq<
    T,
    cross_product_t<
      seq_to_types_t<std::integer_sequence<T,ts...>>,
      seq_to_types_t<Ts>...
    >
  >
{};
模板
结构坐标;
模板
使用coords_t=类型;
模板
结构坐标:
类型_至_seq<
T
交叉积<
顺序至类型,
顺序至类型。。。
>
>
{};
应该爆炸得很好

下一步是构建语法

template<class T, T t0, class Seq>
struct offset_sequence;
template<class T, T t0, class Seq>
using offset_sequence_t=type<offset_sequence<T, t0, Seq>>;
template<class T, T t0, T...ts>
struct offset_sequence<T, t0, std::integer_sequence<T, ts...>>:
  tag<std::integer_sequence<T, (t0+ts)...>>
{};
template<int start, int finish>
using axis_limits = offset_sequence_t<int, start,
  std::make_integer_sequence<finish-start>
>;

template<class T>
using point = std::vector<T>;

template<class T, T...Is>
point<T> make_point( std::integer_sequence<T, Is...> ) {
  return {Is...};
}
template<class...Pts>
std::vector<point<int>> make_space( types<Pts...> ) {
  return { make_point( Pts{} )... };
}
template<class...Ts>
std::vector<point<int>> generate_point_space() {
  return make_space( coords_t<Ts...>{} );
}
模板
结构偏移量_序列;
模板
使用偏移量\u序列\u t=类型;
模板
结构偏移量\u序列:
标签
{};
模板
使用轴限制=偏移顺序;
模板
使用point=std::vector;
模板
点生成点(标准::整数序列){
返回{Is…};
}
模板
标准::向量生成空间(类型){
返回{make_point(Pts{})…};
}
模板
std::向量生成_点_空间(){
返回make_空间(coords_t{});
}
我们有你想要的语法


如果我们愿意,我们可以将东西做成数组和一切
constexpr
。只需更改
make\u point
即可返回
sizeof…(Is)的数组
等等。

我想知道是否可以用tuple\u cat做些什么,然后将tuple转换成可变类型?然后你可以创建一个工厂函数,返回
constepr std::array
。坦白地说,我没有一个立即解决后者的方法,但我非常确定这是可以做到的。我只知道肯定
std::vector
不是一个好办法。@RichardHodges我试着想出了一些办法,它似乎奏效了。另外,看看在编译时与tuplearray转换相关的一些技巧。我想这与另一个答案结合起来就可以了。@RichardHodges也看到了我关于基于
std::array
的s的答案olution.nice运行得很好(进行了一些语法调整)@richardhoges:语法错误已修复。顺便说一句,不要像这里那样使用C-cast(
int(…)
),这很危险。使用
static\u-cast(…)请不要在现有的问题中添加新的、不同的问题。漂亮。谢谢。我多么想读一本关于这门艺术的书。我在C++书架上缺少了这一点,现代模板元编程(功能洞察力)。
template<class Seq>
struct seq_to_types;
template<class Seq>
using seq_to_types_t=type<seq_to_types<Seq>>;
template<class T, T...ts>
struct seq_to_types< std::integer_sequence<T,ts...> >:
  tag< types< std::integral_constant<T,ts>... > >
{};
template<class T, class Rhs>
struct types_to_seq:tag<Rhs>{};
template<class T, class types>
using types_to_seq_t=type<types_to_seq<T,types>>;
template<class T, T...ts>
struct types_to_seq<T, types<std::integral_constant<T, ts>...>>:
  tag<std::integer_sequence<T, ts...>>
{};
template<class T, class...Ts>
struct types_to_seq<T, types<Ts...>>:
  types< types_to_seq_t<T, Ts>... >
{};
template<template<class...>class M, class Seq>
struct mapper;
template<template<class...>class M, class Seq>
using mapper_t=type<mapper<M,Seq>>;
template<template<class...>class M, class...Ts>
struct mapper<M, types<Ts...>>:
  types<M<Ts>...>
{};
template<template<class...>class F, class X>
struct bind_1st {
  template<class...Ts>
  using apply=F<X,Ts...>;
};
template<class...Ts>
struct cross_product:types<types<>>{};
template<class...Ts>
using cross_product_t=type<cross_product<Ts...>>;

template<class...T0s, class...Ts>
struct cross_product<types<T0s...>, Ts...>:cat<
  mapper_t<
    bind_1st<cat_t, types<T0s>>::template apply,
    cross_product_t<Ts...>
  >...
>{};
template<class...Seq>
struct coords;
template<class...Seq>
using coords_t=type<coords<Seq...>>;
template<class T, T...ts, class...Ts>
struct coords< std::integer_sequence<T,ts...>, Ts... >:
  types_to_seq<
    T,
    cross_product_t<
      seq_to_types_t<std::integer_sequence<T,ts...>>,
      seq_to_types_t<Ts>...
    >
  >
{};
template<class T, T t0, class Seq>
struct offset_sequence;
template<class T, T t0, class Seq>
using offset_sequence_t=type<offset_sequence<T, t0, Seq>>;
template<class T, T t0, T...ts>
struct offset_sequence<T, t0, std::integer_sequence<T, ts...>>:
  tag<std::integer_sequence<T, (t0+ts)...>>
{};
template<int start, int finish>
using axis_limits = offset_sequence_t<int, start,
  std::make_integer_sequence<finish-start>
>;

template<class T>
using point = std::vector<T>;

template<class T, T...Is>
point<T> make_point( std::integer_sequence<T, Is...> ) {
  return {Is...};
}
template<class...Pts>
std::vector<point<int>> make_space( types<Pts...> ) {
  return { make_point( Pts{} )... };
}
template<class...Ts>
std::vector<point<int>> generate_point_space() {
  return make_space( coords_t<Ts...>{} );
}