Edu*_*yan 2 c++ templates template-meta-programming constexpr c++17
我正在尝试创建一个可变int参数向量并对+它们执行运算符。
例如:
vec<1,2,3> v1;
vec<4,5> v2;
auto res = v1+v2;
res.print(); // should print 5 7 3 vec<5,7,3> is the result.
Run Code Online (Sandbox Code Playgroud)
现在我想创建 struct vecSum,它将总结给它的向量;
namespace ex {
template<int... N>
struct vec
{
static const int size = sizeof...(N);
void print() {
((std::cout << N << " "), ...);
}
};
template<int...N, int...M, int ...S>
auto add(vec<N...>, vec<M...>, std::index_sequence<S...>) {
constexpr int arr1[sizeof...(S)]{ N... };
constexpr int arr2[sizeof...(S)]{ M... };
return vec<(arr1[S] + arr2[S])...>{};
}
template<int...N, int...M>
auto operator+(vec<N...> v1, vec<M...> v2) {
constexpr size_t size = std::max(sizeof...(N), sizeof...(M));
return add(v1, v2, std::make_index_sequence<size>{});
}
template<typename... Args>
auto all(Args... args) { return (... + args); }
template<typename... Ts>
struct vecSum
{
static constexpr auto res = all(Ts);
};
}
Run Code Online (Sandbox Code Playgroud)
这个测试工作正常:
ex::vec<1, 2, 3> v1;
ex::vec<4, 5> v2;
ex::vec<2> v3;
auto r = ex::all(v1,v2,v3);
r.print();
This prints 7 7 3;
Run Code Online (Sandbox Code Playgroud)
我想要实现的是:
vecSum<vec<1,3,4>, vec<3>, vec<3,4,5>> z;
z::res.print(); and I expect 7 7 9
Run Code Online (Sandbox Code Playgroud)
相反,我有这个错误:
error C2275: 'Ts': illegal use of this type as an expression
error C2119: 'res': the type for 'auto' cannot be deduced from an empty initializer
Run Code Online (Sandbox Code Playgroud)
有人可以提示这里有什么问题吗?我知道我正在尝试将类型参数包Ts作为表达式传递,是否有解决方法或解决方法?
我在您的代码中看到了一些小问题...
Ts在vecSum你定义res如下
static constexpr auto res = all(Ts);
Run Code Online (Sandbox Code Playgroud)
您必须Ts按如下方式展开
// ...............................VVVVV
static constexpr auto res = all(Ts{}...);
Run Code Online (Sandbox Code Playgroud)
std::index_sequence包含std::size_t,不int,值你add()的定义如下
template<int...N, int...M, int ...S>
auto add(vec<N...>, vec<M...>, std::index_sequence<S...>)
Run Code Online (Sandbox Code Playgroud)
但S...价值观是std::size_t
// ........................VVVVVVVVVVV
template<int...N, int...M, std::size_t ...S>
auto add(vec<N...>, vec<M...>, std::index_sequence<S...>)
Run Code Online (Sandbox Code Playgroud)
或模板扣除不起作用。
constexpr 职能如果vecSum必须constexpr,您必须将所有必要的函数(add(), all(), operator+())定义为constexpr。
print() const考虑到您想要打印一个值,该方法vec::print()更好。constconstexpr vecSum::res
以下是完整的编译更正示例
#include <utility>
#include <iostream>
#include <type_traits>
namespace ex {
template<int... N>
struct vec
{
static const int size = sizeof...(N);
void print() const {
((std::cout << N << " "), ...);
}
};
template<int...N, int...M, std::size_t ...S>
constexpr auto add(vec<N...>, vec<M...>, std::index_sequence<S...>) {
constexpr int arr1[sizeof...(S)]{ N... };
constexpr int arr2[sizeof...(S)]{ M... };
return vec<(arr1[S] + arr2[S])...>{};
}
template<int...N, int...M>
constexpr auto operator+(vec<N...> v1, vec<M...> v2) {
constexpr size_t size = std::max(sizeof...(N), sizeof...(M));
return add(v1, v2, std::make_index_sequence<size>{});
}
template<typename... Args>
constexpr auto all(Args... args) { return (... + args); }
template<typename... Ts>
struct vecSum
{
static constexpr auto res = all(Ts{}...);
};
}
int main ()
{
ex::vecSum<ex::vec<1,3,4>, ex::vec<3>, ex::vec<3,4,5>> z;
z.res.print();
}
Run Code Online (Sandbox Code Playgroud)