Ich möchte <<
val.first()
und val.second()
Element in struct Data<std::pair<F, S>
. F
, S
sollte generisch in dem Sinne sein, dass sie alles wie struct Data<int>
spezifizierten Typ sein können. Ich weiß nicht, wie man die Vorlage für struct Data<std::pair<F, S>
in einer Weise schreibt, die es erlaubt, nur struct Data
als F
oder S
zu nehmen.Zugreifen auf Elementfunktionen im Vorlagenbehälter
#include <fstream>
#include <iostream>
#include <sstream>
#include <utility>
template <typename T>
struct Data;
template <typename T>
std::ostream& operator<<(std::ostream& os, const Data<T>& val) {
return val(os);
}
template <typename T>
Data<std::remove_cv_t<T>> data(const T& val) { return { val }; }
template <>
struct Data<int> {
std::ostream& operator()(std::ostream& os) const {
os << val;
return os;
}
const int& val;
};
//More struct Data for templatized types for other specific types
template <typename F, typename S>
struct Data<std::pair<F, S> > {
std::ostream& operator()(std::ostream& os) const {
os << data(val.first()) << data(val.second());
return os;
}
std::pair<F, S> val;
};
//More struct Data templatized for other container types
Der Fehler bei der Prüfung ist:
error: called object type 'int' is not a function or function pointer
os << data(val.first()) << data(val.second());
^~~~~~~~~