【问题标题】:Why does std::begin behave differently when called in a nested function [duplicate]为什么在嵌套函数中调用 std::begin 时表现不同[重复]
【发布时间】:2021-12-31 04:05:18
【问题描述】:

我有一些简单的代码

#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>&);

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

【问题讨论】:

    标签: c++ linux compiler-errors iterator clang++


    【解决方案1】:

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

    void checkNested(int val [10]) 只是void checkNested(int *val) 的语法糖。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-06
      • 2021-06-06
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      相关资源
      最近更新 更多