admin管理员组文章数量:1357273
When writing:
std::vector<double> vec{1.0,2.0};
std::span<const double> s(vec);
s[0]=1.0;
I get a compiler error, as expected:
Cannot assign to return value because function 'operator[]' returns a const value
However, when I go and see the return type of std::span::operator[]
, I only see a reference
return, not a const reference
return, while I was expecting a decltype(auto)
for the return type.
Why do I not get an error that the operator with const reference
is not implemented? How does the compiler figure out the correct answer?
When writing:
std::vector<double> vec{1.0,2.0};
std::span<const double> s(vec);
s[0]=1.0;
I get a compiler error, as expected:
Cannot assign to return value because function 'operator[]' returns a const value
However, when I go and see the return type of std::span::operator[]
, I only see a reference
return, not a const reference
return, while I was expecting a decltype(auto)
for the return type.
Why do I not get an error that the operator with const reference
is not implemented? How does the compiler figure out the correct answer?
1 Answer
Reset to default 6If the type of T
is const double
, then the return of T&
is const double&
.
The qualifiers, like const
or volatile
are part of the type that is held by the span, and are transitive to the returned reference of the operator[]
.
本文标签: cWhy does stdspanoperator not implement a const referenceStack Overflow
版权声明:本文标题:c++ - Why does std::span::operator[] not implement a const reference? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744069393a2585608.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论