2013-06-05 25 views
0

我將一個copy_if綁定到一個將接受一對迭代器的函數對象中。我有一個警告,說我正在返回一個本地地址或臨時地址。如何正確返回本模板代碼中的本地或臨時文件?

我已經知道它來自std :: pair迭代器的綁定成員變量,但我不知道如何糾正它。我不明白爲什麼他們被認爲是暫時的。有人可以向我解釋嗎?

這裏是相關的代碼。該環境是Visual Studio 2010 Ultimate,對Windows 7 Enterprise的幫助不大。

std::vector<My_Type *> destination_container; 

typedef /*A boost multi index iterator that dereferences to a 'My_Type *' */ t_range_iterator; 
typedef std::pair<t_range_iterator, t_range_iterator> t_equal_range; 
typedef std::function<bool(My_Type *)> t_predicate; 
typedef std::back_insert_iterator<std::vector<My_Type *>> t_inserter; 

t_predicate predicate(std::bind(&My_Type::pred, std::placeholders::_1, SOME_CONST)); 

std::function<t_inserter(const t_equal_range)> do_copy_from(std::bind(&std::copy_if<t_range_iterator, t_inserter, t_predicate> 
    , std::bind(&t_equal_range::first, std::placeholders::_1) 
    , std::bind(&t_equal_range::second, std::placeholders::_1) 
    , std::back_inserter(destination_container) 
    , predicate)); 

再後來,代碼會做這樣的事情:

do_copy_from(return_me_an_equal_range_of_My_Type_ptr()); 
do_copy_from(get_me_another_equal_range()); 
do_copy_from(and_so_on_dot_dot_dot()); 
+0

你確定你不想使用一個結構模板operator()函數嗎? –

+0

感謝您的建議;這是務實的。有幾件事我可以做,以解決這個問題,但我不認爲他們是優雅的。最終,我可能會使用這樣的解決方案,並繼續我的生活。 更重要的是,這個問題說明了我想糾正的缺乏理解,所以我不必一直回到這裏來問這個問題。 –

回答

0

沒有足夠多的代碼。但有人猜測這是因爲destination_container容器是本地的 ,而且back_inserter保留對它的引用。

+0

我之前曾經評論過,不同意你的看法,但現在我不太確定......但是爲什麼參考一個地方是一件壞事呢?這不像容器在函數對象的生命週期中將會超出範圍。您是否暗示存儲在後端插入器中的引用超出了範圍? –

+0

如果函數對象是從範圍返回的,那麼是的。 – Dan

+0

但我不知道*這就是發生了什麼。 它也可能被埋在boost多索引迭代器的實現中...我真的不想讀取它。我的意思是我喜歡我一些提升,但是Template元編程是一種閱讀的痛苦。 – Dan

相關問題