Can std::begin work with array parameters and if so, how?

后端 未结 5 2187
慢半拍i
慢半拍i 2020-12-03 02:08

I have trouble using std::begin() and std::end() (from the iterator library) with c-style array parameters.

void SetOr         


        
5条回答
  •  Happy的楠姐
    2020-12-03 02:21

    First off, note that the parameter declaration const double i_point[3] is absolutely equivalent to const double* i_point. That is, the function takes any pointer to double const independent of the number of elements pointed to. As a result, it doesn't know the size and std::begin() and std::end() can't deduce the size (well, std::begin() doesn't really need to deduce the size anyway).

    If you really want to use std::begin() and std::end() you need to pass an array with three element or a reference to such a beast. Since you cannot pass arrays by value, your best bet is to pass it by reference:

    void SetOrigin(double const (&i_point)[3]) {
        // ...
    }
    

    This function only accepts arrays with exactly three elements as arguments: You cannot pass a pointer to three doubles or a part of a bigger array. In return, you can now use std::begin() and std::end().

提交回复
热议问题