簡體   English   中英

back_insert_iterator <>是否可以安全地按值傳遞?

[英]is back_insert_iterator<> safe to be passed by value?

我有一個看起來像這樣的代碼:

struct Data { int value; };
class A {
public:
    typedef std::deque<boost::shared_ptr<Data> > TList;
    std::back_insert_iterator<TList> GetInserter()
    {
        return std::back_inserter(m_List);
    }
private:
    TList m_List;
};
class AA {
    boost::scoped_ptr<A> m_a;
public:
    AA() : m_a(new A()) {}
    std::back_insert_iterator<A::TList> GetDataInserter()
    {
        return m_a->GetInserter();
    }        
};
class B {
    template<class OutIt>
    CopyInterestingDataTo(OutIt outIt)
    {
        // loop and check conditions for interesting data
        // for every `it` in a Container<Data*>
        // create a copy and store it
        for( ... it = ..; .. ; ..) if (...) {
            *outIt = OutIt::container_type::value_type(new Data(**it));
            outIt++; // dummy
        }
    }
    void func()
    {
        AA aa;
        CopyInterestingDataTo(aa.GetDataInserter());
        // aa.m_a->m_List is empty!
    }
};

問題是,即使在CopyInterestingDataTo()之后, A::m_List始終為空。 但是,如果我調試並進入CopyInterestingDataTo() ,則迭代器確實會存儲假定插入的數據!

更新:我找到了罪魁禍首。 我實際上有這樣的東西:

class AA {
    boost::scoped_ptr<A> m_a;
    std::back_insert_iterator<A::TList> GetDataInserter()
    {
        //return m_a->GetInserter(); // wrong
        return m_A->GetInserter(); // this is the one I actually want
    }        
    // ..... somewhere at the end of the file
    boost::scoped_ptr<A> m_A;
};

現在,我應該將哪個答案標記為答案? 真為那些沒有選擇的人感到抱歉,但是你們肯定有一些支持:)

簡短的答案是肯定的,按值傳遞back_insert_iterator是安全的。 長答案:從標准24.4.2 / 3:

插入迭代器滿足輸出迭代器的要求。

和24.1.2 / 1

如果X是可分配類型(23.1),則類或內置類型X滿足輸出迭代器的要求。

最后是23.1中的表64:

表達式 t = u 返回類型 T& 后置條件 t等於u

編輯:一眼您的代碼對我來說就可以 ,您是否100%確定實際上已插入元素? 如果你是我將通過代碼單步,檢查的地址aa.m_a->m_List對象,並把它比作一個存儲在outItCopyInterestingDataTo ,如果他們不一樣的東西的魚腥味。

下面的代碼進行編譯,顯示“ 1”,表示已添加到列表中的一項:

#include <iostream>
#include <deque>
#include "boost/shared_ptr.hpp"
#include "boost/scoped_ptr.hpp"

struct Data { 
    int value; 
    Data( int n ) : value(n) {}
};

struct A {
    typedef std::deque<boost::shared_ptr<Data> > TList;
    std::back_insert_iterator<TList> GetInserter()
    {
        return std::back_inserter(m_List);
    }
    TList m_List;
};

struct AA {
    boost::scoped_ptr<A> m_a;
    AA() : m_a(new A()) {}
    std::back_insert_iterator<A::TList> GetDataInserter()
    {
        return m_a->GetInserter();
    }        
};

struct B {
    template<class OutIt>
    void CopyInterestingDataTo(OutIt outIt)
    {
        *outIt = typename OutIt::container_type::value_type(new Data(0));
        outIt++; // dummy
    }
    int func()
    {
        AA aa;
        CopyInterestingDataTo(aa.GetDataInserter());
        return aa.m_a->m_List.size();
    }
};

int main() {
    B b;
    int n = b.func();    
    std::cout <<  n << std::endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM