C++ 将find()和modify()与boost MultiIndex一起使用时发生类型转换错误
出于某种原因,find()返回的迭代器与用于对其进行操作的modify()实现之间存在类型不匹配。我的代码在这里被逐字复制:C++ 将find()和modify()与boost MultiIndex一起使用时发生类型转换错误,c++,c++11,boost,lambda,boost-multi-index,C++,C++11,Boost,Lambda,Boost Multi Index,出于某种原因,find()返回的迭代器与用于对其进行操作的modify()实现之间存在类型不匹配。我的代码在这里被逐字复制: #include <iostream> #include <string> #include <boost/multi_index_container.hpp> #include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/iden
#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
class Placeholder {
public:
const uint64_t& getPlaceholderTime( void ) const { return ph_time; }
const std::string& getPlaceholderId( void ) const { return ph_id; }
void setNewId( std::string& newId ) { ph_id = newId; }
void setNewTime( uint64_t newTime ) { ph_time = newTime; }
private:
mutable uint64_t ph_time;
std::string ph_id;
};
struct IndexByPlaceholderId { };
typedef boost::multi_index_container<
Placeholder,
boost::multi_index::indexed_by
<boost::multi_index::ordered_non_unique< boost::multi_index::identity<Placeholder> >
,boost::multi_index::ordered_non_unique<
boost::multi_index::const_mem_fun<
Placeholder,
const uint64_t&,
&Placeholder::getPlaceholderTime
>
>
,boost::multi_index::ordered_non_unique<
boost::multi_index::tag<IndexByPlaceholderId>,
boost::multi_index::const_mem_fun<
Placeholder,
const std::string&,
&Placeholder::getPlaceholderId
>
>
>
> currentPlaceholderDatabase;
struct handlePlaceholderTransition {
handlePlaceholderTransition( std::string& newId, uint64_t newTime )
: newId_(newId)
, newTime_(newTime)
{
std::cout << "NewId: " << newId << std::endl;
std::cout << "NewTime: " << newTime << std::endl;
}
void operator()(Placeholder& ph)
{
ph.setNewId( newId_ );
ph.setNewTime( newTime_ );
}
private:
std::string newId_;
uint64_t newTime_;
};
void lambdaHandlePlaceholderTransition( Placeholder& ph, std::string& newId, uint64_t newTime )
{
ph.setNewId( newId );
ph.setNewTime( newTime );
}
int main()
{
static currentPlaceholderDatabase currentDb;
std::string someString = "something";
auto result = currentDb.get<IndexByPlaceholderId>().find( someString );
if( result == currentDb.get<IndexByPlaceholderId>().end() )
{
std::cout << "NOT FOUND\n";
}
else
{
std::string newUpdatedId = "NEW_ID";
currentDb.modify( result, handlePlaceholderTransition( newUpdatedId, 0 ) );
/*
currentDb.modify( result, [](Placeholder& ph) {
std::cout << "Modifying Placeholder\n";
std::string newUpdatedId = "NEW_ID";
lambdaHandlePlaceholderTransition( ph, newUpdatedId, 0 );
});
*/
std::cout << "FOUND!!\n";
}
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
类占位符{
公众:
const uint64_t&getPlaceholderTime(void)const{return ph_time;}
const std::string&getplaceholder id(void)const{return ph_id;}
void setNewId(std::string&newId){ph_id=newId;}
void setNewTime(uint64_t newTime){ph_time=newTime;}
私人:
可变uint64时间;
std::字符串ph_id;
};
结构IndexByPlaceholderId{};
typedef boost::多索引容器<
占位符,
boost::多索引::按索引索引
,boost::多索引::有序非唯一<
boost::多索引::const\u mem\u fun<
占位符,
const uint64_t&,
&占位符::GetPlaceholder时间
>
>
,boost::多索引::有序非唯一<
boost::multi_index::tag,
boost::多索引::const\u mem\u fun<
占位符,
常量std::字符串&,
&占位符::GetPlaceholder ID
>
>
>
>数据库;
结构handlePlaceholderTransition{
handlePlaceholderTransition(标准::字符串和新ID,uint64\u t新时间)
:newId(newId)
,新时间(新时间)
{
标准::cout
>
>
>
>}'
aka显示,除了模板包装的附加“级别”之外,这些类型都是类似的。这个示例源于比较modify和replace时在MultiIndex中显示的示例,因此我无法解释它
我的问题是:
1.)传递此函子以使其使用c++11成功编译的正确语法是什么
及
2.)我可以用这个lambda替换函子吗?我尝试过用这个代码段替换currentDb.modify()行(如上面的注释所示),但得到了相同的错误:
currentDb.modify( result, [](Placeholder& ph) {
std::cout << "Modifying Placeholder\n";
std::string newUpdatedId = "NEW_ID";
lambdaHandlePlaceholderTransition( ph, newUpdatedId, 0 );
});
currentDb.modify(结果,[](占位符和ph){
std::cout问题1:代码的问题是,结果
是索引#2(标记为IndexByPlaceholderId
)和currentdb的迭代器。modify(…)
需要一个索引#0的迭代器。您可以将有问题的行重写为:
currentDb.get<IndexByPlaceholderId>().modify(
result, handlePlaceholderTransition( newUpdatedId, 0 ) );
currentDb.modify( result, [](Placeholder& ph) {
std::cout << "Modifying Placeholder\n";
std::string newUpdatedId = "NEW_ID";
lambdaHandlePlaceholderTransition( ph, newUpdatedId, 0 );
});
currentDb.get<IndexByPlaceholderId>().modify(
result, handlePlaceholderTransition( newUpdatedId, 0 ) );
currentDb.modify(
currentDb.project<0>(result), handlePlaceholderTransition( newUpdatedId, 0 ) );
currentDb.modify( currentDb.project<0>(result), [](Placeholder& ph) {
std::cout << "Modifying Placeholder\n";
std::string newUpdatedId = "NEW_ID";
lambdaHandlePlaceholderTransition( ph, newUpdatedId, 0 );
});