问题描述
我正在尝试为C ++中的Boost.MultiArray实现增量运算符重载函数operator++
。此增量运算符的主要思想是对Boost.MultiArray中的每个元素应用“增量一”操作(++
)。换句话说,每个元素总是可累加的。 is_summable
概念如下创建。
template<typename T>
concept is_summable = requires(T x) { x + x; };
为了处理Boost.MultiArray中的所有类型,包括boost::multi_array
,boost::detail::multi_array::sub_array
和boost::detail::multi_array::const_sub_array
,还创建了一组概念。
template<typename T>
concept is_multi_array = requires(T x)
{
x.num_dimensions();
x.shape();
boost::multi_array(x);
};
template<typename T>
concept is_sub_array = requires(T x)
{
x.num_dimensions();
x.shape();
boost::detail::multi_array::sub_array(x);
};
template<typename T>
concept is_const_sub_array = requires(T x)
{
x.num_dimensions();
x.shape();
boost::detail::multi_array::const_sub_array(x);
};
主要的实验增量运算符重载模板功能:
// Element Increment Operator
template<class T> requires is_summable<T>
auto operator++(T& input)
{
auto output = input;
input++;
return output;
}
template<class T> requires (is_multi_array<T> || is_sub_array<T> || is_const_sub_array<T>)
auto operator++(T& input)
{
boost::multi_array output(input);
for (decltype(+input.shape()[0]) i = 0; i < input.shape()[0]; i++)
{
input[i]++;
}
return output;
}
但是,问题在于以下测试代码中发生了编译错误more than one operator "++" matches these operands
。
// Create a 3D array that is 3 x 4 x 2
typedef boost::multi_array<double,3> array_type;
typedef array_type::index index;
array_type A(boost::extents[3][4][2]);
// Assign values to the elements
int values = 1;
for (index i = 0; i != 3; ++i)
for (index j = 0; j != 4; ++j)
for (index k = 0; k != 2; ++k)
A[i][j][k] = values++;
for (index i = 0; i != 3; ++i)
for (index j = 0; j != 4; ++j)
for (index k = 0; k != 2; ++k)
std::cout << A[i][j][k] << std::endl;
auto test_result = A++;
for (index i = 0; i != 3; ++i)
for (index j = 0; j != 4; ++j)
for (index k = 0; k != 2; ++k)
std::cout << A[i][j][k] << std::endl;
for (index i = 0; i != 3; ++i)
for (index j = 0; j != 4; ++j)
for (index k = 0; k != 2; ++k)
std::cout << test_result[i][j][k] << std::endl;
这个问题有解决方案吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)