[英]range based for loop with const shared_ptr<>
我有一個帶有shared_ptr<>
的容器,例如vector<shared_ptr<string>>
v
並且我想遍歷v
指示常量性。
這段代碼:
vector<shared_ptr<string>> v;
v.push_back(make_shared<std::string>("hallo"));
...
for (const auto &s : v) {
*s += "."; // <<== should be invalid
}
看起來像我想要做的(表明s
是const
)但當然它不會使字符串const
。
有沒有一種優雅的方法來迭代shared_ptr
的容器,這清楚地表明內容不會被修改?
就像是
for (shared_ptr<const string> s : v) {
*s += "."; // <<== will not compile
}
(但由於其他原因,此代碼無法編譯:))
編輯:
我犯了一個錯誤。 最初我聲明了一個引用,這會導致編譯器錯誤
for (shared_ptr<const string> &s : v) { // <<== does not compile
...
}
如果您聲明shared_ptr<const string>
示例有效。 在我看來,這是一個很好的權衡,但這種方式會復制指針,這在具有少量代碼和大容器的循環中可能會很耗時。
這是 C++ 的一個眾所周知的限制,有些人認為這不是限制。
您想迭代const
ly,但不可變指針並不意味着不可變指針。
類型shared_ptr<string>
和類型shared_ptr<const string>
實際上是不相關的。
for (const auto& ptr : v) {
const auto& s = *ptr;
s += "."; // <<== is invalid
}
只是不要修改它。
這是答案。
但首先,講道:
一個指針和它指向的東西是兩個獨立的對象。 要么,none 或兩者都不是 const,而 const 指針只是意味着它不會指向不同的東西。 如果指針是 const,則對象可能不會通過(可能是非常量)指針更改。
話雖如此,我們 (I) 經常編寫使用unique_ptr
或shared_ptr
作為 pimpl 的值語義包裝器對象。 通常我們希望將包裝器的常量性傳播到 impl。
我相信 c++17 將通過它的propagate_const
指針包裝器解決這個問題。
與此同時,構建自己的很簡單:
#include <iostream>
#include <type_traits>
#include <memory>
#include <string>
#include <vector>
namespace traits
{
template<class T> struct pointee;
template<class T, class D>
struct pointee<std::unique_ptr<T, D>> {
using type = T;
};
template<class T>
struct pointee<std::shared_ptr<T>> {
using type = T;
};
template<class T> using pointee_t = typename pointee<T>::type;
}
template<class PointerType>
struct propagate_const
{
using pointer_type = PointerType;
using element_type = traits::pointee_t<pointer_type>;
using value_type = std::decay_t<element_type>;
using reference = value_type&;
using const_reference = const value_type&;
propagate_const(pointer_type p) : _ptr(std::move(p)) {}
const_reference operator*() const {
return *_ptr;
}
auto operator*()
-> std::enable_if_t<not std::is_const<element_type>::value, reference>
{
return *_ptr;
}
private:
pointer_type _ptr;
};
template<class PointerType>
auto make_propagating_pointer(PointerType&& p)
{
return propagate_const<PointerType>(std::forward<PointerType>(p));
}
int main()
{
using namespace std;
vector<propagate_const<shared_ptr<string>>> v;
v.emplace_back(make_shared<string>("hello"));
for (const auto& p : v)
{
// *p += " there"; // compile error
cout << *p;
cout << endl;
}
for (auto& p : v)
{
*p += " there";
cout << *p;
cout << endl;
}
return 0;
}
預期輸出:
hello
hello there
這個很簡單,只支持operator*
但是添加一套完整的operator很簡單。 請注意,當指針為 const 時,我禁用了可變訪問。
參考: http : //en.cppreference.com/w/cpp/experimental/propagate_const
只是為了好玩,這里有一個shared_string
類的完整示例,它在內部使用shared_ptr
並正確傳播shared_string
。
#include <iostream>
#include <type_traits>
#include <memory>
#include <string>
#include <vector>
template<class PointerType>
struct propagate_const
{
using pointer_type = PointerType;
using element_type = std::remove_reference_t<decltype(*std::declval<PointerType&>())>;
using reference = element_type&;
using const_reference = const element_type&;
propagate_const(pointer_type p) : _ptr(std::move(p)) {}
const_reference operator*() const {
return *_ptr;
}
auto operator*()
-> std::enable_if_t<not std::is_const<element_type>::value, reference>
{
return *_ptr;
}
private:
pointer_type _ptr;
};
template<class PointerType>
auto make_propagating_pointer(PointerType&& p)
{
return propagate_const<PointerType>(std::forward<PointerType>(p));
}
struct shared_string
{
shared_string(std::string s) : _impl(std::make_shared<std::string>(std::move(s))) {};
shared_string(std::shared_ptr<std::string> sp) : _impl(sp) {};
shared_string(propagate_const<std::shared_ptr<std::string>> sp) : _impl(sp) {};
auto& operator += (const std::string& s) {
*_impl += s;
return *this;
}
friend std::ostream& operator<<(std::ostream& os, const shared_string& ss) {
return os << *(ss._impl);
}
private:
propagate_const<std::shared_ptr<std::string>> _impl;
};
template<class T, std::enable_if_t<std::is_const<T>::value>* = nullptr >
std::string check_const(T&)
{
return std::string("const");
}
template<class T, std::enable_if_t<not std::is_const<T>::value>* = nullptr >
std::string check_const(T&)
{
return std::string("not const");
}
int main()
{
using namespace std;
// a vector of mutable shared_strings
vector<shared_string> v;
// a vector of immutable shared_strings
vector<const shared_string> cv;
// make a shared_string
v.emplace_back(make_shared<string>("hello"));
// refer to the *same one* in cv
cv.emplace_back(v[0]);
for (const auto& p : v)
{
// *p += " there"; // immutable reference to mutable shared string - not allowed
cout << check_const(p) << " " << p;
cout << endl;
}
for (auto& p : v)
{
cout << check_const(p) << " " << p;
p += " there"; // mutable reference to mutable shared string - allowed
cout << " becomes " << p;
cout << endl;
}
for (auto&p : cv)
{
cout << check_const(p) << " " << p;
// p += " world"; // p is actually immutable because cv contains immutable objects
cout << endl;
}
return 0;
}
預期輸出:
const hello
not const hello becomes hello there
const hello there
我會使用模板方法
template <class T,class F>
void forEach(const std::vector<std::shared_ptr<T>>& vec, F&& f){
for (const auto& ptr : vec){
if (ptr){
f(std::cref(*ptr));
}
}
}
我在那里放了一個 lambda 函數,編譯器可能無論如何都會內聯它,所以這里沒有性能損失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.