Home Accessing member functions in templated container
Reply: 1

Accessing member functions in templated container

Davar Published in 2017-12-07 01:25:36Z

I want to be able to << val.first() and val.second() element in struct Data<std::pair<F, S>. F, S should be generic in the sense that they can be anything specified like struct Data<int> (specific type). I don't know how to write the template for struct Data<std::pair<F, S> in a way that allows it to only take struct Data as F or S.

#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

The error when tested is:

 error: called object type 'int' is not a function or function pointer
                    os << data(val.first()) << data(val.second());
O'Neil Reply to 2017-12-07 01:42:40Z

first and second are two (public) member variables of std::pair, they are not member functions.

os << data(val.first()) << data(val.second());


os << data(val.first) << data(val.second);
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300901 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO