Question

I have a type which contains an array internally. I need a function which returns the number of array members in A or 1 if the type is not A.

Here's the code:

#include <cstdio>
template<typename T, unsigned n>
struct A
{
    T values[n];
};
template<typename T>
unsigned count_components()
{
    return 1;//all types except 'A' should have 1 component
}
template<typename T, unsigned n>
unsigned count_components<A<T, n> >()//specialize count_components for 'A'
{
    return n;
}

int main()
{
    printf("%d\n", count_components<A<float, 4> >());//should be 4
    printf("%d\n", count_components<float>());//should be 1
    return 0;
}

G++ error:

test.cpp:13:37: error: function template partial specialization ”count_components<A<T, n> >” is not allowed
unsigned count_components<A<T, n> >()//specialize count_components for 'A'
                                    ^
Was it helpful?

Solution

When I have functions, I prefer to stay with functions (which is more beneficial with member functions, because you still have access to *this).

template<typename T, unsigned n>
unsigned count_components_switch(boost::mpl::identity<A<T, n>>)
{
    return n;
}

template<typename T>
unsigned count_components_switch(boost::mpl::identity<T>)
{
    return 1;
}

template<typename T>
unsigned count_components()
{
    return (count_components_switch)(boost::mpl::identity<T>());
}

OTHER TIPS

Functions can not be specialized partially. Instead, you can use classes, which can be partially specialized:

#include <cstdio>
template<typename T, unsigned n>
struct A
{
    T values[n];
};

template<typename T>
struct component_counter
{
    static unsigned count()
    {
        return 1;
    }
};
template<typename T, unsigned n>
struct component_counter<A<T, n> >
{
    static unsigned count()
    {
        return n;
    }
};
int main()
{
    printf("%d\n", component_counter<A<float, 4> >::count());//should be 4
    printf("%d\n", component_counter<float>::count());//should be 1
    return 0;
}

And in this case the count() does not actually have to be a function at all! You can do it like this:

#include <cstdio>
template<typename T, unsigned n>
struct A
{
    T values[n];
};

template<typename T>
struct component_counter
{
    static const unsigned count=1;
};
template<typename T, unsigned n>
struct component_counter<A<T, n> >
{
    static const unsigned count=n;
};
int main()
{
    printf("%d\n", component_counter<A<float, 4> >::count);//should be 4
    printf("%d\n", component_counter<float>::count);//should be 1
    return 0;
}

Which is less code. The caveat is that count has to be an integral type for this to work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top