Skip to content
Snippets Groups Projects
Commit a7e376af authored by Tüske's avatar Tüske
Browse files

PHP list() II. – a nehezebbik fele

parent 10a6fe9a
No related branches found
No related tags found
No related merge requests found
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <tuple>
template<class T>
void unpack(std::istringstream& s, T& arg)
template<class ...T>
class container;
template<class ...T>
container<T...> list(T&...args);
template<class ...T>
class container
{
s>>arg;
private:
std::tuple<T&...> tu;
public:
container(T&...args):tu{args...}{}
template<class ...F>
friend container<T...> list(T&...args);
template<int I>
typename std::enable_if<I==1>::type readhelper(std::vector<std::string>& s)
{
std::istringstream is{s[I-1]};
is>>std::get<I-1>(tu);
}
template<class T>
void unpack(std::istringstream&& s, T& arg)
template<int I>
typename std::enable_if<I!=1>::type readhelper(std::vector<std::string>& s)
{
s>>arg;
readhelper<I-1>(s);
std::istringstream is{s[I-1]};
is>>std::get<I-1>(tu);
}
template<class T, class ...V>
void unpack(std::istringstream& s, T& arg , V& ...args)
public:
void operator=(std::vector<std::string>&& s)
{
s>>arg;
unpack(s,args...);
readhelper<std::tuple_size<decltype(tu)>::value>(s);
}
};
template<class T, class ...V>
void unpack(std::istringstream&& s, T& arg , V& ...args)
template<class ...T>
container<T...> list(T&...args)
{
s>>arg;
unpack(s,args...);
return container<T...>{args...};
}
template<class ...T>
void unpack(std::string s, T& ...args)
std::vector<std::string> explode(std::string s)
{
unpack(std::istringstream{s},args...);
std::vector<std::string> vec;
std::istringstream d{s};
while(d)
{
std::string st;
d>>st;
vec.push_back(st);
}
return std::move(vec);
}
int main(int argc, char **argv) {
int a;
double b;
std::string c;
unpack("3 4.5 bla", a, b, c); // a = 3; b = 4.5;
std::cout<<a<<":"<<b<<":"<<c;
int a=0;
double b=0;
list(a, b) = explode("3 4.5");
std::cout<<a<<":"<<b;
return 0;
}
\ No newline at end of file
{"compileCppOptions":["-std\u003dc++11"],"requirements":[]}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment