为什么 std::begin 在嵌套函数中调用时表现不同

Why does std::begin behave differently when called in a nested function

我有一些简单的代码

#include<iterator>

int main() {
    int y[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    auto a = std::begin(y);
    std::cout << *a << std::endl;
    return 0;
}

按预期打印出 1

但是,如果我这样做:

void checkNested(int val [10]) {
    auto a = std::begin(val);
    std::cout << *a << std::endl;

}

int main() {
    int y[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
    checkNested(y);
    return 0;
}

我从 clang++g++ 得到编译失败。 从 clang++ 具体来说,我得到:


    auto a = std::begin(input);
             ^~~~~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/initializer_list:89:5: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'int *'
    begin(initializer_list<_Tp> __ils) noexcept
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:48:5: note: candidate template ignored: substitution failure [with _Container = int *]: member reference base type 'int *' is not a structure or union
    begin(_Container& __cont) -> decltype(__cont.begin())
    ^                                           ~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:58:5: note: candidate template ignored: substitution failure [with _Container = int *]: member reference base type 'int *const' is not a structure or union
    begin(const _Container& __cont) -> decltype(__cont.begin())
    ^                                                 ~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:87:5: note: candidate template ignored: could not match '_Tp [_Nm]' against 'int *'
    begin(_Tp (&__arr)[_Nm])
    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:104:31: note: candidate template ignored: could not match 'valarray<type-parameter-0-0>' against 'int *'

  template<typename _Tp> _Tp* begin(valarray<_Tp>&);
                              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/range_access.h:105:37: note: candidate template ignored: could not match 'valarray<type-parameter-0-0>' against 'int *'
  template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);

只是想知道我是否真的遗漏了一些明显的东西,因为我希望它们的功能相同。 谢谢

数组不能按值传递,所以你的数组在传递给 checkNested() 时会衰减为指针,而 std::begin() 没有为指针定义,因此出现错误。

void checkNested(int val [10]) 只是 void checkNested(int *val).

的语法糖

如果您改为通过引用传递数组,则代码将起作用:

void checkNested(int (&val) [10])