如何从使用类型隐藏的索引形成的多索引容器中按名称获取索引
how to get index by name from multi index container formed with indices using type hiding
我正在尝试为 indexed_by 部分使用类型隐藏来制作多个索引容器。
所以我首先制作要在容器中使用的类型并制作一些标签
struct Log_file_Dur_Sig_F_Map_struct //Duration_Signal_Folder_Map_struct >>>log file destination
{
int Id;
std::string Duration;
std::string FolderDuration;
std::string FolderDurationSpecefied;
std::string FolderDurationPathString;
std::string FolderDurationSpecefiedPathString;
Log_file_Dur_Sig_F_Map_struct(const std::string& duration, const std::string& folderdurationspecefied, std::string const& folderdurationspecefiedpathstring ) : Duration(duration), FolderDurationSpecefied(folderdurationspecefied), FolderDurationSpecefiedPathString(folderdurationspecefiedpathstring)
{
if (duration == "AllTime")
{
Id = 0;
}
else if (duration == "Yearly")
{
Id = 1;
}
else if (duration == "Monthly")
{
Id = 2;
}
else if (duration == "Daily")
{
Id = 3;
}
/*
switch (duration)
{
case "AllTime": printf("Choice is AllTime");
break;
case "Yearly": printf("Choice is 2");
break;
case "Monthly": printf("Choice is 3");
break;
case "Daily": printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
*/
}
bool operator<(const Log_file_Dur_Sig_F_Map_struct& e)const { return Id<e.Id; }
};
/* tags for accessing the corresponding indices of Log_file_Dur_Sig_F_Map_struct_set */
struct Id {};
struct Duration_d {};
struct FolderDuration {};
struct FolderDurationSpecefied {};
/*
struct FolderDurationPathString {};
struct FolderDurationSpecefiedPathString {};
struct Duration_FolderDuration {};
struct FolderDuration_FolderDurationSpecefied {};
struct FolderDurationSpecefiedPathString {};
struct FolderDurationSpecefiedPathString {};
struct FolderDurationSpecefiedPathString {};
*/
struct D_FD_FDS {};
/*
*NB: The use of derivation here instead of simple typedef is explained in
* Compiler specifics : type hiding.
*/
struct D_FD_FDS_key :composite_key <
Log_file_Dur_Sig_F_Map_struct,
BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, Duration),
BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, FolderDuration),
BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, FolderDurationSpecefied)
> {};
struct Log_file_Dur_Sig_F_Map_struct_set_indices :
indexed_by <
ordered_unique<
//tag<Id>, BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, int, Id)>,
tag<Id>, member<Log_file_Dur_Sig_F_Map_struct, int, &Log_file_Dur_Sig_F_Map_struct::Id> >,
ordered_non_unique<
tag<Duration_d>, BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, Duration)>,
ordered_non_unique<
tag<FolderDuration>, BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, FolderDuration)>,
ordered_non_unique<
tag<FolderDurationSpecefied>, BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, FolderDurationSpecefied)>,
ordered_unique<
tag<D_FD_FDS>, D_FD_FDS_key >
>
{};
typedef multi_index_container<
Log_file_Dur_Sig_F_Map_struct,
Log_file_Dur_Sig_F_Map_struct_set_indices
> Log_file_Dur_Sig_F_Map_struct_set;
然后我为这个容器的一个索引创建 typedef
typedef Log_file_Dur_Sig_F_Map_struct_set::index<D_FD_FDS>::type Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS;
但是当我尝试使用 Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS
获取迭代器时,visual studio 的智能感知告诉我这个索引的类型是 mpl::void
我认为这与类型隐藏有关,因为现在多索引 typedef 没有扩展的 indexed_by 列表..
我如何从使用 indexed_by 的派生结构定义的多索引容器中检索某些索引???
我没有发现您的代码有任何问题。例如,以下完美运行(完整示例 here):
typedef Log_file_Dur_Sig_F_Map_struct_set::index<D_FD_FDS>::type Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS;
typedef Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS::iterator Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS_iterator;
int main()
{
Log_file_Dur_Sig_F_Map_struct_set s;
Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS& sd=s.get<D_FD_FDS>();
Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS_iterator first=sd.begin(),last=sd.end();
assert(first==last);
}
因此,我的猜测是 Intellisense 由于所涉及的类型的复杂性而令人窒息。这原则上不应该阻止你继续编译。
我正在尝试为 indexed_by 部分使用类型隐藏来制作多个索引容器。 所以我首先制作要在容器中使用的类型并制作一些标签
struct Log_file_Dur_Sig_F_Map_struct //Duration_Signal_Folder_Map_struct >>>log file destination
{
int Id;
std::string Duration;
std::string FolderDuration;
std::string FolderDurationSpecefied;
std::string FolderDurationPathString;
std::string FolderDurationSpecefiedPathString;
Log_file_Dur_Sig_F_Map_struct(const std::string& duration, const std::string& folderdurationspecefied, std::string const& folderdurationspecefiedpathstring ) : Duration(duration), FolderDurationSpecefied(folderdurationspecefied), FolderDurationSpecefiedPathString(folderdurationspecefiedpathstring)
{
if (duration == "AllTime")
{
Id = 0;
}
else if (duration == "Yearly")
{
Id = 1;
}
else if (duration == "Monthly")
{
Id = 2;
}
else if (duration == "Daily")
{
Id = 3;
}
/*
switch (duration)
{
case "AllTime": printf("Choice is AllTime");
break;
case "Yearly": printf("Choice is 2");
break;
case "Monthly": printf("Choice is 3");
break;
case "Daily": printf("Choice is 3");
break;
default: printf("Choice other than 1, 2 and 3");
break;
}
*/
}
bool operator<(const Log_file_Dur_Sig_F_Map_struct& e)const { return Id<e.Id; }
};
/* tags for accessing the corresponding indices of Log_file_Dur_Sig_F_Map_struct_set */
struct Id {};
struct Duration_d {};
struct FolderDuration {};
struct FolderDurationSpecefied {};
/*
struct FolderDurationPathString {};
struct FolderDurationSpecefiedPathString {};
struct Duration_FolderDuration {};
struct FolderDuration_FolderDurationSpecefied {};
struct FolderDurationSpecefiedPathString {};
struct FolderDurationSpecefiedPathString {};
struct FolderDurationSpecefiedPathString {};
*/
struct D_FD_FDS {};
/*
*NB: The use of derivation here instead of simple typedef is explained in
* Compiler specifics : type hiding.
*/
struct D_FD_FDS_key :composite_key <
Log_file_Dur_Sig_F_Map_struct,
BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, Duration),
BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, FolderDuration),
BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, FolderDurationSpecefied)
> {};
struct Log_file_Dur_Sig_F_Map_struct_set_indices :
indexed_by <
ordered_unique<
//tag<Id>, BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, int, Id)>,
tag<Id>, member<Log_file_Dur_Sig_F_Map_struct, int, &Log_file_Dur_Sig_F_Map_struct::Id> >,
ordered_non_unique<
tag<Duration_d>, BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, Duration)>,
ordered_non_unique<
tag<FolderDuration>, BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, FolderDuration)>,
ordered_non_unique<
tag<FolderDurationSpecefied>, BOOST_MULTI_INDEX_MEMBER(Log_file_Dur_Sig_F_Map_struct, std::string, FolderDurationSpecefied)>,
ordered_unique<
tag<D_FD_FDS>, D_FD_FDS_key >
>
{};
typedef multi_index_container<
Log_file_Dur_Sig_F_Map_struct,
Log_file_Dur_Sig_F_Map_struct_set_indices
> Log_file_Dur_Sig_F_Map_struct_set;
然后我为这个容器的一个索引创建 typedef
typedef Log_file_Dur_Sig_F_Map_struct_set::index<D_FD_FDS>::type Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS;
但是当我尝试使用 Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS
获取迭代器时,visual studio 的智能感知告诉我这个索引的类型是 mpl::void
我认为这与类型隐藏有关,因为现在多索引 typedef 没有扩展的 indexed_by 列表..
我如何从使用 indexed_by 的派生结构定义的多索引容器中检索某些索引???
我没有发现您的代码有任何问题。例如,以下完美运行(完整示例 here):
typedef Log_file_Dur_Sig_F_Map_struct_set::index<D_FD_FDS>::type Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS;
typedef Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS::iterator Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS_iterator;
int main()
{
Log_file_Dur_Sig_F_Map_struct_set s;
Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS& sd=s.get<D_FD_FDS>();
Log_file_Dur_Sig_F_Map_struct_set_by_D_FD_FDS_iterator first=sd.begin(),last=sd.end();
assert(first==last);
}
因此,我的猜测是 Intellisense 由于所涉及的类型的复杂性而令人窒息。这原则上不应该阻止你继续编译。