Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single struct adjusting methods to different data types

Tags:

c++

c++11

I have a four very similar structs (very simple to illustrate the problem) differing in type of the arguments

using state_type = std::vector<double>;

struct foo {
    double _x;
    double _y;

    foo (double x, double y) : _x{x}, _y{y} {}

    void operator() (const state_type &v, state_type &u) const {
       for (auto i=0; i<v.size(); i++)
          u[i] = x * v[i] + y * v[i];
    }
}

struct foo {
    state_type _x;
    double _y;

    foo (state_type x, double y) : _x{x}, _y{y} {}

    void operator() (const state_type &v, state_type &u) const {
       for (auto i=0; i<v.size(); i++)
          u[i] = x[i] * v[i] + y * v[i];
    }
}

struct foo {
    double _x;
    state_type _y;

    foo (double x, state_type y) : _x{x}, _y{y} {}

    void operator() (const state_type &v, state_type &u) const {
       for (auto i=0; i<v.size(); i++)
          u[i] = x * v[i] + y[i] * v[i];
    }
}

struct foo {
    state_type _x;
    state_type _y;

    foo (state_type x, state_type y) : _x{x}, _y{y} {}

    void operator() (const state_type &v, state_type &u) const {
       for (auto i=0; i<v.size(); i++)
          u[i] = x[i] * v[i] + y[i] * v[i];
    }
}

Is there a way to use just a single struct that automatically chooses the right one based on the type of the arguments?

like image 911
Bociek Avatar asked Mar 05 '26 00:03

Bociek


1 Answers

I would use a template and a helper function to deduce the type, then specialize the operator() for various type combinations. A very simple example to illustrate the point:

#include <iostream>
#include <vector>

using state_type = std::vector<double>;

template<typename T, typename S>
struct X
{
    T _a;
    S _b;
    X(T a, S b): _a(a), _b(b){}

    // specialize this for different types
    void operator()(const state_type& v, state_type& u) const 
    {
        std::cout << "default implementation\n";
    }
};

// specializations of operator() for <state_type, double> 
template<>
void X<state_type, double>::operator()(const state_type& v, state_type& u) const
{
    std::cout << "<state_type, double> functor\n";
}

template<typename T, typename S>
X<T,S> make_X(const T& a, const S& b)
{
    return X<T,S>(a,b); // or even return {a, b};
}

int main()
{
    state_type st;
    auto foo_double_statetype = make_X(st, 42.); // makes X<state_type, double>
    foo_double_statetype(st, st); // calls the specialized operator()

    auto foo_int_int = make_X(42, 42); // makes X<int, int>
    foo_int_int(st, st); // calls the default operator() (non-specialized)
}

Live on Coliru

like image 99
vsoftco Avatar answered Mar 07 '26 12:03

vsoftco