我將一個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());
你確定你不想使用一個結構模板operator()函數嗎? –
感謝您的建議;這是務實的。有幾件事我可以做,以解決這個問題,但我不認爲他們是優雅的。最終,我可能會使用這樣的解決方案,並繼續我的生活。 更重要的是,這個問題說明了我想糾正的缺乏理解,所以我不必一直回到這裏來問這個問題。 –