Home Accessing member functions in templated container
Reply: 0

Accessing member functions in templated container

user1780
1#
user1780 Published in April 27, 2018, 6:52 am

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());
                               ^~~~~~~~~
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO