templates - std::tuple objects or rvalue references? -


i have small "plugin system" (not sure right name). allow store objects (plugins), , call methods each of them. approach have absolutely no overhead on iterating/object pass/callback call.

// need set, because in c++11 auto can static const.  // , may need change them later. #ifndef set     #define set(name, value) decltype(value) name = value #endif  plugin1 plugin1(param1, param2); plugin2 plugin2(param1, param2); plugin3 plugin3(param1, param2);      set(plugins, std::tie(           // pay attention here. store &, not objects     plugin1,      plugin2,     plugin3 )); 

then, when need call function each of plugins, or each of iterate through plugins @ compile time (i check generated asm code, call or inline do_it, without else) , call callback function (i not show iterate code here, it's trivial):

struct call{     float k=0;      template<typename t, int index>        // lambda function not efficient this. tested -o2 clang, gcc 4.8     inline void do_it(t &&t){                        std::cout << "value = " <<t << " ; " << "id = " << index << std::endl;     } }; 

and if need specific plugin, directly use plugin1,plugin2, plugin3. no need call std::get<>. plus ide highlight available plugins when type them.

well, , question is. ok store rvalue references on objects, or have store objects directly in tuple? this:

set(plugins, std::tuple(     plugin1(param1, param2),      plugin2(param1, param2),     plugin3(param1, param2) )); 

when iterate, pass plugin usual reference:

struct call{     float k=0;      template<typename t, int index>        // lambda function not efficient this. tested -o2 clang, gcc 4.8     inline void do_it(t &t){                        std::cout << "value = " <<t << " ; " << "id = " << index << std::endl;     } }; 

with approach have additional move constructor calls each element in tuple. previous version free.

i ask this, because, read data locality , worry how data placed in memory. maybe not have think about...


update

1) without "set" we'll have this:

// in global scope auto plugins = std::tie(          plugin1,          plugin2,         plugin3     ); 

and it's ok. till in global scope. want put "plugins" class member, compiler demand constexpr static, , become:

struct myclass{     static const plugin1 plugin1(param1, param2);     static const plugin2 plugin2(param1, param2);     static const plugin3 plugin3(param1, param2);       static const auto plugins = std::tie(              plugin1,              plugin2,             plugin3         ); }; 

here is possible have "auto" member variable?

2) life time... first case &&. hold objects separately, , our "plugins" hold rvalue references objects:

http://coliru.stacked-crooked.com/a/d2a9b33dd9f15369


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -