Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I distinguish overloads of templates with non-type parameters?

Here are two template functions, that differ only in their template parameters. The rest of the parameters are exactly the same.

    template<int module>
    void template_const(int &a,int & b){
            a = a & module;
            b = b % module;
    }

    template<bool x>
    void template_const(int &a,int & b){
            int w;
            if (x){
                    w = 123;
            }
            else w = 512;
            a = a & w;
            b = b % w;
    }

When I try to call them like this

template_const<true>(a,b)

or

template_const<123>(a,b)

the compiler tells me that the call is ambiguous. How can I call these two functions?

like image 880
worldterminator Avatar asked Jun 26 '13 07:06

worldterminator


1 Answers

As @jogojapan pointed out, the problem is that the compiler cannot order these two functions, i.e. there is not one that is more specialized than the other. As explained in §14.5.6.2, when a call to an overloaded function template is ambiguous, the compiler uses a partial ordering between the various overloads to select the most specialized one.

To order the overloads, the compiler transforms each one of them and performs template argument deduction to see if one is more specialized than another one (there is a short explanation at the end of this answer). In your case, the two overloads are equivalent (or not comparable): template<int> void template_const(int &,int &) is not more specialized than template<bool> void template_const(int &, int &), and vice-versa.

Therefore, the compiler cannot select one over the other, hence generating an ambiguous call error.


If you are ok with explicitely specifying the type of the parameter you want to pass, you can use partial template specialization as follow:

template<typename T, T param>
struct template_const_impl;

template <int module>
struct template_const_impl<int, module>
{
    static void apply(int &a, int &b)
    {
        a = a & module;
        b = b % module;
    }
};

template<bool x>
struct template_const_impl<bool, x>
{
    static void apply(int &a, int &b)
    {
        const int w = x ? 123 : 512;
        a = a & w;
        b = b % w;
    }
};

template <typename T, T param>
void template_const(int &a, int &b)
{
    return template_const_impl<T, param>::apply(a, b);
}

int main()
{
    int i = 512, j = 256;
    template_const<int, 123>(i, j);
    template_const<bool, true>(i, j);
}

This is not ideal, but it don't think there is a cleaner solution unless you can use C++11 and are willing to rely on some macros, in which case you can simplify the calling code a bit (idea taken from @Nawaz in this answer):

#define TEMPLATE_CONST(x) template_const<decltype(x), x>

int main()
{
    int i = 512, j = 256;
    TEMPLATE_CONST(123)(i, j);
    TEMPLATE_CONST(true)(i, j);
}
like image 161
Luc Touraille Avatar answered Sep 24 '22 14:09

Luc Touraille