加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用std :: get on std :: array可以提供更好的性能吗?

发布时间:2020-12-16 10:37:31 所属栏目:百科 来源:网络整理
导读:标准库类模板std :: array T,N具有成员访问器功能 constexpr const T operator[]( size_type n ) const; 以及非成员访问者功能模板 template size_t I,class T,size_t N constexpr const T get( const arrayT,N a ) noexcept 在C 17中,所有的operator []重载
标准库类模板std :: array< T,N>具有成员访问器功能

constexpr const T& operator[]( size_type n ) const;

以及非成员访问者功能模板

template< size_t I,class T,size_t N >
constexpr const T& get( const array<T,N>& a ) noexcept

在C 17中,所有的operator []重载都已经成为constexpr,所以我想知道std :: get的剩余优点是什么(如果有的话).例如.在这样的程序中:

int main()
{
    auto a = std::array<int,3> { 1,2,3 };
    constexpr auto idx = 0;
    std::cout << a[idx] << 'n';
    std::cout << std::get<idx>(a) << 'n';
}

任何体面的编译器都应该能够为operator []和get传播常量索引值0.

问题:std :: get对std :: array有什么好处,那运算符[]不会?

解决方法

结构化绑定需要获取.

其他通用元组代码也将使用它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读