jeudi 3 décembre 2020

std::initializer_list for parameter lists on C++11 literal operators

According to cppreference, only const char *, unsigned long long, long double , char and variations are allowed on literal operators:

// using std::chrono::seconds operator""s(unsigned long long);
auto sec = 30s;
// using std::string operator""s(const char*, std::size_t);
auto str = "hello"s;

I wonder, if we could take std::initializer_list as a parameters, we can expand it to make and STL container literal possible:

// using std::vector<int> operator""v(std::initializer_list<int>);
auto vec = {1, 2, 3, 4, 5}v;
   
// using std::list<double> operator""l(std::initializer_list<double>);
auto list = {1.0, 2.0, 3.0, 4.0, 5.0}l;

Why does a literal operator have such a limitation on its parameter list? Is there some defect or complexity to take a std::initializer_list as a literal operator’s parameter?

Aucun commentaire:

Enregistrer un commentaire