Templates: Specializing by Value?

Hey everyone,

So I know that you can specialize functions and methods by type, i.e.
1
2
template <typename X> foo(arg1,arg2);
template <int> foo(arg3,arg4,arg5);


What I'd like to know is if it's possible to specify by value, via typedef or some other such hack. The reason I'd like to do this is so that I could overload via the template, which would allow me greater flexibility when determining the behavior of this function. Thanks in advance!
Could you post an example of what you did and what you would like to do?
I think you should use it like this:
1
2
template <>
int foo<int>(arg1,arg2);

But you should use the same arguments as were in the general implementation
Last edited on
@seymore: something like the following would be nice:
1
2
3
/*@type C: a class that both functions return*/
template<VALUE_ONE> C* foo (arg1,arg2,arg3);
template<VALUE_TWO> C* foo (arg4,arg5,arg6);

The nice thing about this approach is that ideally, I could specialize for two different values with the same arguments, and get different results; this would also allow the difference to be resolved at compile time, rather than by way of a predicate at run time, i.e.

1
2
/*same args as VALUE_ONE, but internally different*/
template<VALUE_THREE> C*foo(arg1,arg2,arg3);


@Bazzy: thanks for the input. What I'm looking for, though, is not a way to specify by TYPE, but by a VALUE.

Also, I just had an idea. I'll get back to you all soon : )

EDIT: So much for that, heh. Suggestions still open!
Last edited on
eg,

1
2
3
4
5
6
7
8
9
template< size_t N >
struct factorial {
   enum { value_ = N * factorial<N-1>::value_ };
};

template<>
struct factorial<0> {
   enum { value_ = 1 };
};


Are you just trying to do some template metaprogramming, or do you actually want some functionality to perform your task? It sounds like you just need a function that takes 4 parameters!
@jsmith: Exactly! Thank you for posting that!

@seymore: the problem with 4 parameters is that a) it's not guaranteed that the args will be the same, and b) even if they are, I'd rather not have the different resolved at runtime.

Either way, jsmith posted what I was getting at, so now I'll give a shot. Thanks to all contributers!
Topic archived. No new replies allowed.