提升multi_index获得依赖类型
boost multi_index get on dependent type
我遇到了一个问题,对于依赖类型,如果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不再依赖。
相关文章:
- 如何从C++中的依赖类型中获得它所依赖的类型
- 当基类是依赖类型时,这是一个缺陷吗
- 通过依赖类型使用非类型模板参数的单类型模板参数类模板的部分专用化
- 为什么不需要在 C++20 中的依赖类型之前指定"typename"?
- 为什么在以下情况下不需要为依赖类型使用typename
- 提升multi_index获得依赖类型
- 继承依赖类型定义而不使用结构
- 依赖于依赖类型的非静态数据成员的非限定名称
- 依赖类型自动扣除
- 具有unordered_map的依赖类型
- 难以理解C++依赖类型与当前实例化上的内容
- 扣除其他类型的依赖类型
- C++:从依赖类型"Inferring"模板类型
- 编译器是否也将所有参数依赖类型实例化,即使超载函数的参数计数不匹配
- 限定依赖类型的类型定义
- 为什么对于依赖类型,单词 'typedef' 后面需要'typename'?
- 如何从模板化的类方法返回依赖类型
- 无法定义依赖类型定义的成员
- 如何在任意依赖类型上专门化模板
- 依赖类型不明确的 c++11 可变参数函数模板重载是否不明确