如何使用类型隐藏从由索引形成的多索引容器中按名称获取索引



我正在尝试使用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???

我认为你的代码没有任何问题。例如,以下内容完美运行(此处为完整示例(:

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正在窒息。这原则上不会阻止您继续编译。

最新更新