提升multi_index获得依赖类型

boost multi_index get on dependent type

本文关键字:依赖 类型 index multi 提升      更新时间:2023-10-16

我遇到了一个问题,对于依赖类型,如果C是依赖类型,boost::multi_index::multi_index_container<...>::get<C>()中断。如果它不是依赖的,则不会发生相同的情况。MCVE,g++ 7.1.0(如果你声明is_same_v<>,也是6.3.0(:

#include <type_traits>                                                                                             
#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>                                                                           
#include <boost/multi_index/random_access_index.hpp>                                                               
#define MEMBER(T, M)        boost::multi_index::member<T, decltype(T::M), &T::M>                                   
#define TAGNMEMBER(T, M)    boost::multi_index::tag<MEMBER(T, M)>, MEMBER(T, M)                                    
namespace ns1 {                                                                                                    
struct Elem {                                                                                                  
size_t id_;                                                                                                
bool operator<(const Elem& rhs) const { return id_ < rhs.id_; }                                            
};                                                                                                             
using Cont = boost::multi_index::multi_index_container<                                                        
Elem,                                                                                                      
boost::multi_index::indexed_by<                                                                            
boost::multi_index::ordered_unique<boost::multi_index::identity<Elem>>,                                
boost::multi_index::ordered_unique<TAGNMEMBER(Elem, id_)>                                              
>                                                                                                          
>;                                                                                                             
}                                                                                                                  
namespace ns2 {                                                                                                    
//    void f(ns1::Cont& cont) // always works                                                                      
template<typename C>                                                                                           
void f(C& cont)                                                                                                
{                                                                                                              
ns1::Cont& c = cont;                                                                                       
static_assert(std::is_same_v<decltype(cont), ns1::Cont&>);                                                 
cont.get<MEMBER(ns1::Elem, id_)>(); // v1 - error if dependent type                                        
//c.get<MEMBER(ns1::Elem, id_)>();  // v2 - always works                                                   
}                                                                                                              
}                                                                                                                  

int main()                                                                                                         
{                                                                                                                  
ns1::Cont cont;                                                                                                
cont.insert({0});                                                                                              
ns2::f(cont);                                                                                                  
}

你能指教吗?

指示get是一个模板:

cont.template get<MEMBER(ns1::Elem, id_)>(); // now works

它适用于c,因为你显式声明了它的类型(ns1::Cont(,因此,get不再依赖。