In my project there is need to append initializer list at runtime.
I have figured the way to have initializer_list std::initializer_list<std::pair<std::string, std::string>> at runtime in my project, but not able to figure out way to append this list if user passing multiple no. of pairs.
dummy code implementation of that part is below:
#include <iostream>
#include <string.h>
#include <initializer_list>
#define __FILENAME__ strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__
#define S1(x) #x
#define S2(x) S1(x)
#define LOCATION std::string(__FILENAME__) + ":" + std::string(S2(__LINE__)) + ":" + std::string(__PRETTY_FUNCTION__)
#define ATTR_PAIR std::pair<std::string, std::string>("Log.Details",LOCATION)
int main()
{
std::string s = "op";
std::string s1 = "de";
std::string s2 = "en";
auto pa = std::make_pair(s,s1);
auto pb = std::make_pair(s,s2);
auto list = {pb,pa};
std::initializer_list<std::pair<std::string, std::string>> L({list}) ;
std::initializer_list<std::pair<std::string, std::string>> I = {ATTR_PAIR,*(L .begin()),*(L.begin()+1)}; //is there any generic way to append this with as much pairs user passes in list
for(auto it=I.begin();it!=I.end();++it)
{
std::cout<<(it->first)<<std::endl;
std::cout<<(it->second)<<std::endl;
}
}
my need is to append initializer I with as much pair I have in list . code which I wrote here, I have passed (L.begin()+1) to have 2nd pair. Is there any other generic way to do so???
The length of a std::initializer_list
cannot be chosen dynamically at runtime and list
's length here is not a compile-time usable property.
std::initializer_list
is the wrong tool for this. It should only be used as parameters to functions or implicitly in e.g. a range-for loop. In any case only directly to store a {/*...*/}
braced-initializer-list temporarily to be processed further e.g. into a proper container to copy elements from.
If you need a sequence container with runtime-determined dimensions, use std::vector
instead. Elements can be added to it with e.g. push_back
/emplace_back
/insert
/emplace
member functions and it has a constructor taking an iterator range.