vendredi 6 mai 2016

Is it possible to use generic programming without templates?

Say for example I have a function that returns a value from a 2-dimensional array:

float get_2d_noise(const point& p)
{
    return map2D[p.x][p.y];
}

The point is a class that I've defined as part of my library:

struct point
{
    int x;
    int y;
}

Aside from doing this:

template<typename T>
float get_2d_noise(const T& p)
{
    return noisemap[p.x][p.y];
}

Is it possible to get the same effect? I.e., create get_2d_noise in such a way that anything with an x and y member will work? (preferably catching errors at compile time, rather than runtime).

Aucun commentaire:

Enregistrer un commentaire