Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.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++ std::accumulate中的转换无效_C++_C++11_Accumulate_Stdarray - Fatal编程技术网

C++ std::accumulate中的转换无效

C++ std::accumulate中的转换无效,c++,c++11,accumulate,stdarray,C++,C++11,Accumulate,Stdarray,我有一个类,用一个静态函数(每个字段的最小值)表示N维度中的一个点 我得到一个错误: error: cannot convert ‘Point<float, 3ul>’ to ‘float’ in initialization adimx::Point<T,N>::Point(Args&&... args) : std::array<T,N>{{args...}} 错误:初始化时无法将“点”转换为“浮点” adimx::Point::Poin

我有一个类,用一个静态函数(每个字段的最小值)表示
N
维度中的一个点

我得到一个错误:

error: cannot convert ‘Point<float, 3ul>’ to ‘float’ in initialization
adimx::Point<T,N>::Point(Args&&... args) : std::array<T,N>{{args...}}
错误:初始化时无法将“点”转换为“浮点”
adimx::Point::Point(Args&&…Args):std::array{{{Args…}

这是因为
std::acculate
实现与我的构造函数不兼容吗?

由于构造函数通过重载解决方案获胜,您可以提供所需构造函数的默认版本:

Point(Point&) = default;
Point(const Point&) = default;
Point(Point&&) = default;
Point& operator=(const Point&) = default;

这里有一种方法可以约束该构造函数,这样它只有在所有参数都隐式转换为
float
时才参与重载解析:

template<bool... > class bool_pack;
template<bool... b>
using all_true = std::is_same<bool_pack<true, b...>, bool_pack<b..., true>>;

template<typename... Args,
         class = typename std::enable_if<all_true<std::is_convertible<Args, float>::value...>::value>::type>
Point(Args&&... args) : std::array<T,N>{{args...}} {}
模板类bool\u包;
模板
使用all_true=std::is_same;
模板
点(Args&&…Args):std::array{{{Args…}}{}
新解决方案: 没关系,但为什么呢?

<强> AS N4260 12.9 31.3 C++ + StRADAD表示:>P/> 当尚未绑定到引用的临时类对象 (12.2)将被复制/移动到具有相同 cv不合格类型,复制/移动操作可由 将临时对象直接构造到 省略复制/移动

因此,使用三个浮点直接调用
构造函数,不调用复制构造函数。因此,调用转发引用变量参数构造函数不将点对象作为参数传递,这就是编译错误所在


缺点是每次使用
累积
函数时都需要传入右值,并且不能是左值。

变量构造函数正在劫持复制构造函数调用。约束它。@T.C.那是maes sens.我如何约束它?@Amxx隐式指定复制构造函数:
Point(const Point&)=defaultT.C.说的是对的。Forwarding引用为copy ctor调用生成更好的匹配,因此它由重载解析选择。仍然为此类生成复制构造函数。它不必显式默认(用户声明),因为您是重复计算
数组[0]
。如果使用
array[0]
作为初始值,则从
array+1
开始累积。或者,使用
点(0,0,0)
作为初始值。很抱歉,您错了。Forwarding引用为copy ctor调用生成了更好的匹配,因此它会被重载解析选择。仍然为此类生成复制构造函数。它不必显式默认(用户声明)
error: cannot convert ‘Point<float, 3ul>’ to ‘float’ in initialization
adimx::Point<T,N>::Point(Args&&... args) : std::array<T,N>{{args...}}
Point(Point&) = default;
Point(const Point&) = default;
Point(Point&&) = default;
Point& operator=(const Point&) = default;
template<bool... > class bool_pack;
template<bool... b>
using all_true = std::is_same<bool_pack<true, b...>, bool_pack<b..., true>>;

template<typename... Args,
         class = typename std::enable_if<all_true<std::is_convertible<Args, float>::value...>::value>::type>
Point(Args&&... args) : std::array<T,N>{{args...}} {}
template <bool J>
using disable_if=enable_if<!J>;

template <typename T,typename... Tail> struct getHead
{
    typedef typename decay<T>::type type;
};

template<typename... Args,typename = typename disable_if<is_same<typename getHead<Args...>::type,Point<T,N> >::value >::type >
    Point(Args&&... args) : std::array<T,N> {{args...}}
    {
        //...
    }
template<typename... Args>
        Point(Args... args) : std::array<T,N> {{args...}} {}//remove &&
Point<float,3> min = std::accumulate(array, array+100, Point<float,3>(0,0,0), Point<float,3>::min);