gpt4 book ai didi

c++ - boost multi_index 获得依赖类型

转载 作者:行者123 更新时间:2023-11-28 01:44:58 26 4
gpt4 key购买 nike

<分区>

我遇到了一个问题,对于依赖类型,boost::multi_index::multi_index_container<...>::get<C>()如果 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);
}

能请教一下吗?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com