std::move_iterator<Iter>::operator*,->
來自 cppreference.com
reference operator*() const; |
(1) | (C++11 起) (自 C++17 起為 constexpr) |
pointer operator->() const; |
(2) | (C++11 起) (自 C++17 起為 constexpr) (C++20 中已棄用) |
返回對當前元素的右值引用或指標。
目錄 |
[編輯] 返回值
2)
current
[編輯] 注意
operator-> 已被棄用,因為解引用其結果可能產生左值。這可能導致意外行為。
[編輯] 示例
執行此程式碼
#include <iomanip> #include <iostream> #include <iterator> #include <string> #include <vector> void print(auto rem, const auto& v) { for (std::cout << rem; const auto& e : v) std::cout << std::quoted(e) << ' '; std::cout << '\n'; } int main() { std::vector<std::string> p{"alpha", "beta", "gamma", "delta"}, q; print("1) p: ", p); for (std::move_iterator it{p.begin()}, end{p.end()}; it != end; ++it) { it->push_back('!'); // calls -> string::push_back(char) q.emplace_back(*it); // *it <- overload (1) } print("2) p: ", p); print("3) q: ", q); std::vector v{1, 2, 3}; std::move_iterator it{v.begin()}; // *it = 13; // error: using rvalue as lvalue }
可能的輸出
1) p: "alpha" "beta" "gamma" "delta" 2) p: "" "" "" "" 3) q: "alpha!" "beta!" "gamma!" "delta!"
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2106 | C++11 | operator* 將返回一個懸空 引用,如果 * current 產生一個純右值 |
返回物件 在這種情況下 |
[編輯] 參閱
透過索引訪問元素 (public member function) |