我有multi_index_container
一个struct Person
:
struct Person {
std::string firstName;
std::string lastName;
int id;
bool operator <(const Person& rhs) const {return id < rhs.id;}
};
//index tags
struct BaseIndexTag {};
struct FirstName : BaseIndexTag {};
struct LastName : BaseIndexTag {};
struct ID : BaseIndexTag {};
namespace bmi = boost::multi_index;
typedef boost::multi_index_container<
Person,
bmi::indexed_by<
//PinsConnection operator < ordering
bmi::ordered_unique<bmi::tag<ID>, bmi::identity<Person> >,
//order by first name
bmi::ordered_non_unique<bmi::tag<FirstName>, bmi::member<Person, std::string, &Person::firstName> >,
//Order by last name
bmi::ordered_non_unique<bmi::tag<LastName>, bmi::member<Person, std::string, &Person::lastName> >
>
> PersonMultiSet;
和 boost 的包装类iterator_adaptor
:
template <
typename I //Internal iterator type
, typename C //Container class
>
class IteratorAdaptor : public boost::iterator_adaptor <
IteratorAdaptor<I,C>, //Derived
I, //Base
boost::use_default, //Value
boost::forward_traversal_tag> //Traversal
{
private:
friend class boost::iterator_core_access;
friend C;
public:
IteratorAdaptor()
: typename IteratorAdaptor<I,C>::iterator_adaptor_() {}
explicit IteratorAdaptor(I& b)
: typename IteratorAdaptor<I,C>::iterator_adaptor_(b) {}
};
最后,我为 编写了一个包装类PersonMultiSet
,以便向该类的用户公开一个干净的接口并避免 boostmulti_index_container
有点繁琐的接口:
class PersonContainer {
public:
typedef IteratorAdaptor<PersonMultiSet::index<ID>::type::iterator, PersonContainer> IDIterator;
typedef IteratorAdaptor<PersonMultiSet::index<ID>::type::const_iterator, PersonContainer> IDConstIterator;
typedef IteratorAdaptor<PersonMultiSet::index<FirstName>::type::iterator, PersonContainer> FirstNameIterator;
typedef IteratorAdaptor<PersonMultiSet::index<FirstName>::type::const_iterator, PersonContainer> FirstNameConstIterator;
typedef IteratorAdaptor<PersonMultiSet::index<LastName>::type::iterator, PersonContainer> LastNameIterator;
typedef IteratorAdaptor<PersonMultiSet::index<LastName>::type::const_iterator, PersonContainer> LastNameConstIterator;
template <typename T> //tag
void foo(IteratorAdaptor<PersonMultiSet::index<T>::type::iterator, PersonContainer> &it)
{
//...
}
private:
PersonMultiSet people;
};
在 VS2010 下编译此代码会在函数 foo 的声明中产生以下错误:
1> main.cpp
1>main.cpp(70): warning C4346: 'boost::multi_index::multi_index_container<Value,IndexSpecifierList>::index<T>::boost::multi_index::multi_index_container<Value1,IndexSpecifierList1,Allocator1>::index<Tag>::type::iterator' : dependent name is not a type
1> with
1> [
1> Value=Person,
1> IndexSpecifierList=boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<ID>,boost::multi_index::identity<Person>>,boost::multi_index::ordered_non_unique<boost::multi_index::tag<FirstName>,boost::multi_index::member<Person,std::string,pointer-to-member(0x0)>>,boost::multi_index::ordered_non_unique<boost::multi_index::tag<LastName>,boost::multi_index::member<Person,std::string,pointer-to-member(0x20)>>>
1> ]
1> prefix with 'typename' to indicate a type
1>main.cpp(70): error C2923: 'IteratorAdaptor' : 'boost::multi_index::multi_index_container<Value,IndexSpecifierList>::index<T>::boost::multi_index::multi_index_container<Value1,IndexSpecifierList1,Allocator1>::index<Tag>::type::iterator' is not a valid template type argument for parameter 'I'
1> with
1> [
1> Value=Person,
1> IndexSpecifierList=boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<ID>,boost::multi_index::identity<Person>>,boost::multi_index::ordered_non_unique<boost::multi_index::tag<FirstName>,boost::multi_index::member<Person,std::string,pointer-to-member(0x0)>>,boost::multi_index::ordered_non_unique<boost::multi_index::tag<LastName>,boost::multi_index::member<Person,std::string,pointer-to-member(0x20)>>>
1> ]
1>
1>Build FAILED.
似乎编译器不允许IteratorAdaptor<PersonMultiSet::index<T>::type::iterator
在第一次使用时确定类型(我什至在尝试实例化PersonContainer
或调用 foo 之前就收到了这个错误)。我究竟做错了什么?