Ho unTradurre uno std :: tupla in un parametro di modello pacchetto
typedef std::tuple<A, B> TupleType;
e vorrei utilizzare l'elenco delle classi per un "modello".
Supponiamo che io sono:
template<typename... args>
std::tuple<args...> parse(std::istream &stream) {
return std::make_tuple(args(stream)...);
}
e che posso usare con successo con:
auto my_tuple = parse<A, B>(ifs);
è possibile evitare di dover specificare l'elenco di classe A, B se ho già un
typedef std::tuple<A,B> TupleType;
dove la lista A, B è già presente?
un esempio:
#include <cstdlib> // EXIT_SUCCESS, EXIT_FAILURE
#include <iostream> // std::cerr
#include <fstream> // std::ifstream
#include <tuple> // std::tuple
class A {
public:
A(std::istream &); // May throw FooBaarException
};
class B {
public:
B(std::istream &); // May throw FooBaarException
};
template<typename... args>
std::tuple<args...> parse(std::istream &stream) {
return std::make_tuple(args(stream)...);
}
int main() {
std::ifstream ifs;
ifs.exceptions(ifstream::eofbit | ifstream::failbit | ifstream::badbit);
int res = EXIT_FAILURE;
try {
ifs.open("/some/file/path", std::ios::in | std::ios::binary);
auto my_tuple = parse<A, B>(ifs); // my_tuple is of the type std::tuple<A,B>
/* Here do something interesting with my_tuple */
res = EXIT_SUCCESS;
} catch (ifstream::failure e) {
std::cerr << "error: opening or reading file failed\n";
} catch (FooBaarException e) {
std::cerr << "error: parsing in a constructor failed\n";
}
return res;
}
Sembra che nel costruttore si voglia leggere dalle stringhe. Tieni presente che l'ordine con cui vengono chiamati i costruttori non è specificato per la tua implementazione di 'parse'. –
@ JohannesSchaub-litb, punto interessante. Anche questo può essere fatto: http://liveworkspace.org/code/MTk2Nj$0 a condizione che i tipi di componenti siano distinti (possibile ma troppo lungo per essere mostrato come un esempio con tipi duplicati). – rici