在编译时推导整个向量 space
deduce entire vector space at compile time
受到这个问题的启发:
我开始想有没有一种形式的模板代码可以,从这个说法:
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> };
在编译时构造一个名为 space 的向量,其中每个 x、y、z 包含一个点,其中 begin(X) <= x < end(X)... y 和 z 等。
顺序不重要。
generate_point_space<>
的 return 类型应该是 std::initializer_list<int>
或类似的编译时构造的序列。我不想生成对 push_back()
的一系列调用。那太容易了:)
struct point
将具有以下形式的构造函数:
point::point(int x, int y, int z)
整数的单一维度很简单(下面的代码)。问题的多维方面今天超出了我
#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;
}
当前输出:
{ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4 }
我在找什么:
{ { -10, -10, -10 }, { -10, -10, -9 } .... { 9, 9, 8 }, { 9, 9, 9 } }
std::vector
constructors are not constexpr
, so you're out of luck here. You cannot return them as constexpr
from a factory function, the vector is always constructed at run-time. Perhaps you should try std::array
?像
#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 << " ";
}
您可以执行以下操作:
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>());
}
一些用于处理类型列表的元编程助手:
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<>
{};
一种在值序列和类型序列之间进行映射的方法。我发现类型更容易使用:
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>... >
{};
现在我们可以使用 std::integer_sequence<int, 1,2,3>
并生成 types< std::integral_constant<int,1>, std::integral_constant<int,2>, std::integral_constant<int,3> >
,在我看来这更容易处理。我们甚至可以映射回来。
这需要一个 types<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>...>
{};
mapper_t< some_metafunction, types<blah...>>
将通过 some_metafunction
映射每个 blah
以生成新的类型列表。
接下来,一种获取类型函数并将第一个参数绑定到 X
的方法:
template<template<class...>class F, class X>
struct bind_1st {
template<class...Ts>
using apply=F<X,Ts...>;
};
可以轻松使用叉积(连同 cat_t
和 mapper_t
):
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...>{} );
}
我们有您想要的语法。
如果我们愿意,我们可以把东西变成数组和所有东西 constexpr
。只需将 make_point
更改为 return 一个 sizeof...(Is)
之类的数组。
受到这个问题的启发:
我开始想有没有一种形式的模板代码可以,从这个说法:
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> };
在编译时构造一个名为 space 的向量,其中每个 x、y、z 包含一个点,其中 begin(X) <= x < end(X)... y 和 z 等。
顺序不重要。
generate_point_space<>
的 return 类型应该是 std::initializer_list<int>
或类似的编译时构造的序列。我不想生成对 push_back()
的一系列调用。那太容易了:)
struct point
将具有以下形式的构造函数:
point::point(int x, int y, int z)
整数的单一维度很简单(下面的代码)。问题的多维方面今天超出了我
#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;
}
当前输出:
{ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4 }
我在找什么:
{ { -10, -10, -10 }, { -10, -10, -9 } .... { 9, 9, 8 }, { 9, 9, 9 } }
std::vector
constructors are not constexpr
, so you're out of luck here. You cannot return them as constexpr
from a factory function, the vector is always constructed at run-time. Perhaps you should try std::array
?像
#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 << " ";
}
您可以执行以下操作:
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>());
}
一些用于处理类型列表的元编程助手:
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<>
{};
一种在值序列和类型序列之间进行映射的方法。我发现类型更容易使用:
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>... >
{};
现在我们可以使用 std::integer_sequence<int, 1,2,3>
并生成 types< std::integral_constant<int,1>, std::integral_constant<int,2>, std::integral_constant<int,3> >
,在我看来这更容易处理。我们甚至可以映射回来。
这需要一个 types<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>...>
{};
mapper_t< some_metafunction, types<blah...>>
将通过 some_metafunction
映射每个 blah
以生成新的类型列表。
接下来,一种获取类型函数并将第一个参数绑定到 X
的方法:
template<template<class...>class F, class X>
struct bind_1st {
template<class...Ts>
using apply=F<X,Ts...>;
};
可以轻松使用叉积(连同 cat_t
和 mapper_t
):
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...>{} );
}
我们有您想要的语法。
如果我们愿意,我们可以把东西变成数组和所有东西 constexpr
。只需将 make_point
更改为 return 一个 sizeof...(Is)
之类的数组。