lundi 4 janvier 2016

Calculate the euclidean distance via a functions with a variable parameter list

This example shows how to use variadic templates to calculate the squared distance. Unfortuanately, this will not work with sqrt() if I want to calculate the euclidean distance. How I can calculate the euclidean distance with a variable number of function parameters.

    /*
     * Calculated the euclidian distance of the obtained parameter list
     */
    template<class T>
    T squared_distance(const T &val) {
        static_assert(std::is_floating_point<T>::value || std::is_integral<T>::value, 
            "ERROR - squared_distance(): template parameter not of type integer or float\n");

        return std::pow(val, 2);
    }

    template<class T, class... Params> 
    T squared_distance(const T &first, const Params&... parameters) {
        return std::pow(first, 2) + squared_distance(parameters...);
    }

Aucun commentaire:

Enregistrer un commentaire