c++ - How to make aliases of member function or variable of specific class(like an STL container) -


when using std::pair or std::map, need use "first" or "second" access data. 2 variable name not have clear meanings of store other co-workers did not write code. if can make aliases "first" or "second", enhance readability.

for example, following code

static const std::map<std::string, std::pair<std::string, pfconvert>> comm_map = {  // keyword->         (caption,                   function) {std::string("1"), {std::string("big5 utf16le"), &fileconvert_big5toutf16le}}, {std::string("2"), {std::string("utf16le utf8"), &fileconvert_utf16letoutf8}}, {std::string("3"), {std::string("utf8 big5"), &fileconvert_utf8tobig5}} };  auto itertoexe = comm_map.find(strtranstype); itertoexe->second.second(); 

the itertoexe->second.second(); has bad readability.

so try use inherit give aliases following

template<typename pfcomm> class ccommcontent : public std::pair<std::string, pfcomm> { public:     std::string &strcaption = std::pair<std::string, pfcomm>::first;     pfcomm &pfcomm = std::pair<std::string, pfcomm>::second; };  template<typename pfcomm> class ccommpair : public std::pair<std::string, ccommcontent<pfcomm>> { public:     std::string &strpattern = std::pair<std::string, ccommcontent<pfcomm>>::first;     ccommcontent<pfcomm> commcontent = std::pair<std::string,ccommcontent<pfcomm>>::second; };  template<typename pfcomm> class ccommmap : public std::map<std::string, ccommcontent<pfcomm>, std::less<std::string>, std::allocator<ccommpair<pfcomm>>> {}; 

but comes issue: have declare ctors, though call base ctors, still not seems smart method. want make aliases.

a simple way use macro ...... bypass type checking. when using nested structure, may nightmare when debug.

any advice or discussion appreciated.

why not use own struct own element names?

struct mypair {     std::string strcaption;     pfcomm pfcomm; }; 

with c++11 can create new objects of it:

mypair{std::string("big5 utf16le"), &fileconvert_big5toutf16le}} 

and if define own operator<, can have std::set work map:

bool operator<(const mypair& a, const mypair& b) {     return a.strcaption < b.strcaption; }  typedef std::set<mypair> mypairmap; 

naturally, can nest custom structs form more complex nested pairs, although in case might want consider flat triplet instead:

struct commmapentry {      std::string number;      std::string caption;      pfcomm pfcomm; }; bool operator<(const mypair& a, const mypair& b) {     return a.number<b.number; } static const std::set<commmapentry> comm_map; 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -