4

Is there an elegant way to specialize a template based on one of its template parameters?

Ie.

template<int N> struct Junk {
    static int foo() {
        // stuff
        return Junk<N - 1>::foo();
    }
};

// compile error: template argument '(size * 5)' involves template parameter(s)
template<int N> struct Junk<N*5> {
    static int foo() {
        // stuff
        return N;
    }
};

template<> struct Junk<0> {
    static int foo() {
        // stuff
        return 0;
    }
};

Ie. I am trying to specialize a template based on the parameter being divisible by 5. The only way I can seem to do it is like below:

template<int N> struct JunkDivisibleBy5 {
    static int foo() {
        // stuff
        return N;
    }
};

template<int N> struct Junk {
    static int foo() {
        // stuff
        if ((N - 1) % 5 == 0 && N != 1)
            return JunkDivisibleBy5<N - 1>::foo();
        else
            return Junk<N - 1>::foo();
    }
};


template<> struct Junk<0> {
    static int foo() {
        // stuff
        return 0;
    }
};

But this is significantly less elegant, and also necessitates instantiation of all templates even if the template argument shouldn't require it.

6 Answers 6

13

How's this:

#include <iostream>
using namespace std;

template < typename T, T N, T D >
struct fraction {
    typedef T value_type;
    static const value_type num = N;
    static const value_type denom = D;
    static const bool is_div = (num % denom == 0);
};

template< typename T, T N, T D, bool P >
struct do_if {
    static void op() { cout << N << " NOT divisible by " << D << endl; }
};

template< typename T, T N, T D >
struct do_if< T, N, D, true > {
    static void op() { cout << N << " divisible by " << D << endl; }
};

template < int N >
void foo() {
    typedef fraction< int, N, 5 > f;
    do_if< typename f::value_type, f::num, f::denom, f::is_div >::op();
}

int main() {
    foo< -5 >();
    foo< -1 >();
    foo< 0 >();
    foo< 1 >();
    foo< 5 >();
    foo< 10000005 >();
    return 0;
}
8
  • I compiled it, ran it and verified the test results. I included the entire program so others can do the same.
    – jwfearn
    Nov 6, 2008 at 17:17
  • not with g++ 4.1.3 meta_programming.cpp:10: error: explicit specialization in non-namespace scope struct my<N> meta_programming.cpp:10: error: enclosing class templates are not explicitly specialized Nov 6, 2008 at 17:32
  • CLARIFICATION: I compiled it with Visual C++ 2008, ran it and verified the test results.
    – jwfearn
    Nov 6, 2008 at 18:00
  • Sorry @David Nehme, I don't use g++. If you have a g++ compatible solution, please post it because I'd like to learn more about what I'm doing wrong.
    – jwfearn
    Nov 6, 2008 at 18:07
  • 2
    put typename before f::value_type. f::value_type is a dependent type. May 31, 2009 at 6:08
7

Using D programming language templates, one could write it as:

struct Junk(int N)
{
    static int foo()
    {
        static if (N == 0)
            return 0;
        else static if ((N % 5) == 0)
            return N;
        else
            return Junk!(N - 1).foo();
    }
}

static if's are executed at compile time.

1
  • I shouldn't upvote this, but I did because it suddenly makes me want to learn D :) Sep 10, 2015 at 11:09
5

All calculations could be made in compile-time:

#include <iostream>

template<int N> struct Junk {
    enum { IsDivisibleBy5 = (N % 5 == 0) };
    template<bool D> struct JunkInternal {
        enum { Result = Junk<N-1>::Result };
    };
    template<> struct JunkInternal<true> {
        enum { Result = N };
    };
    enum { Result = JunkInternal<IsDivisibleBy5>::Result };
};

int main(int, char**)
{
    std::cout << Junk< 0 >::Result << std::endl;
    std::cout << Junk< 7 >::Result << std::endl;
    std::cout << Junk< 10 >::Result << std::endl;

    return 0;
}
4

Code

template<int A, bool = !(A % 5)>
struct select : select<A-1> { };

template<int A>
struct select<A, true> { static int const value = A; };

template<>
struct select<0, true> { static int const value = 0; };

int main() {
    std::cout << select<1>::value; // 0
    std::cout << select<7>::value; // 5
    std::cout << select<10>::value; // 10
}

Keep the divisor variable

template<int A, int D, bool = !(A % D)>
struct select : select<A-1, D> { };

template<int A, int D>
struct select<A, D, true> { static int const value = A; };

template<int D>
struct select<0, D, true> { static int const value = 0; };

int main() {
    std::cout << select<1, 3>::value; // 0
    std::cout << select<7, 3>::value; // 6
    std::cout << select<10, 3>::value; // 9
}
2

Inheritance works quite well:

template<int N> struct Junk : private JunkBase < N % 5 > { };

template<int N> struct JunkBase {
    static int foo() {
        // stuff
        return Junk<N - 1>::foo();
    }
};
template< > struct JunkBase<0> {
    static int foo() {
        return 0;
    }
};

You might need to pass N to JunkBase::foo, if you need N/5 too.

0

I would hardly call it elegant, but here's my version of your code using only templates for computation (along with a test thing) --

#include <iostream>

template < int N > struct JunkDivBy5 {
    static int foo() {
        return N;
    }
};

template < int N > struct Junk {
    template < int N1 > struct _JunkCond {
        enum { val = ( N1 != 1 && ( N1 - 1 ) % 5 == 0 ) ? 1 : 0 };
    };

    template < int M, int N1 > struct _JunkBranch { /* Error */ };

    template < int N1 > struct _JunkBranch< 1, N1 > {
        typedef JunkDivBy5< N1 - 1 > Type;
    };

    template < int N1 > struct _JunkBranch< 0, N1 > {
        typedef Junk< N1 - 1 > Type;
    };

    static int foo() {
        return _JunkBranch< _JunkCond< N >::val, N >::Type::foo();
    }
};

template <> struct Junk< 0 > {
    static int foo() {
        return 0;
    }
};

int main( int argc, char *argv[] ) {
    std::cout << Junk< 0 >::foo() << std::endl;
    std::cout << Junk< 5 >::foo() << std::endl;
    std::cout << Junk< 7 >::foo() << std::endl;
    std::cout << Junk< 25 >::foo() << std::endl;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.