0
votes

I am trying to make a templated class that has different behavior based on if it's template argument is a primitive type or a container. Basically, if the template parameter is a container, I want operations to push_back on the container, but if it's a primitive type, I want the class to assign to it. See main below for an example of desired behavior.

Here's my attempt at making this work, but I can't get it to compile after trying various things. Can anybody tell me what's going wrong here? Thanks!

Attempt 1

#include <vector>

template<typename V>
struct store_to
{
    static void call(V& d, V const& v)
    {
        d = v;
    }
};

template<typename V, template<typename> class C>
struct store_to< C<V> >
{
    static void call(C<V>& d, V const& val)
    {
        d.push_back(val);
    }
};

template<typename V>
struct get_element_type
{
    typedef V value_type;
};

template<typename V, template<typename> class C>
struct get_element_type< C<V> >
{
    typedef typename C<V>::value_type value_type;
};

template<class T>
class holder
{
public:
    typedef T value_type;
    typedef typename get_element_type<T>::value_type element_type;

    holder() { }
    ~holder() { }

    void store(element_type const& value)
    { store_to<T>::call(_data, value); }

    void store_default()
    { store_to<T>::call(_data, _default_value); }

    void set_default(element_type const& value)
    { _default_value = value; }

    const value_type& data() const
    { return _data; }

    const element_type& default_value() const
    { return _default_value; }

private:
    value_type _data;
    element_type _default_value;
};

int main()
{
    holder<int> x;
    x.set_default(1);
    x.store_default(); // e.g. _data = _default_value;
    x.store(5); // e.g. _data = 5;

    holder< std::vector<int> > y;
    y.set_default(5);
    y.store_default(); // e.g. _data.push_back(_default_value);
    y.store(0); // e.g. _data.push_back(0);

    return 0;
}

Attempt 2

Here's a modification that works for std::list and std::vector, though it's still not quite as generic as I'd like. Is there any way to make this work for any class C that has a push_back function and a value_type typedef without specializing the template a bunch more times?

#include <vector>
#include <list>

template<typename V>
struct store_to
{
    typedef V outer_type;
    typedef V inner_type;

    static void call(outer_type& d, inner_type const& v)
    { d = v; }
};

template<typename V>
struct store_to< std::vector<V> >
{
    typedef typename std::vector<V> outer_type;
    typedef typename outer_type::value_type inner_type;

    static void call(outer_type& d, inner_type const& val)
    { d.push_back(val); }
};

template<typename V>
struct store_to< std::list<V> >
{
    typedef typename std::list<V> outer_type;
    typedef typename outer_type::value_type inner_type;

    static void call(outer_type& d, inner_type const& val)
    { d.push_back(val); }
};

template<class T>
class holder
{
public:
    typedef typename store_to<T>::outer_type outer_type;
    typedef typename store_to<T>::inner_type inner_type;

    holder() { }
    ~holder() { }

    void store(inner_type const& value)
    { store_to<T>::call(_data, value); }

    void store_default()
    { store_to<T>::call(_data, _default_value); }

    void set_default(inner_type const& value)
    { _default_value = value; }

    const outer_type& data() const
    { return _data; }

    const inner_type& default_value() const
    { return _default_value; }

private:
    outer_type _data;
    inner_type _default_value;
};

int main()
{
    holder<int> x;
    x.set_default(1);
    x.store_default(); // e.g. _data = _default_value;
    x.store(5); // e.g. _data = 5;

    holder< std::vector<int> > y;
    y.set_default(5);
    y.store_default(); // e.g. _data.push_back(_default_value);
    y.store(0); // e.g. _data.push_back(0);

    return 0;
}
1
There is no real way to check for something being a container but this checks for a fundamental type – aaronman
Concepts will provide the required check, if You can wait till 2014 ;) – Skeen

1 Answers

0
votes

You can pass an insert iterator to your template instead of the whole container. When you call operator = on an insert iterator, it adds a new element to the container. You will need to include <iterator> header and there are a few different types available.

std::vector<int> container;
MyTemplateFunction(back_inserter(container));