I’d like to specialize following template:
template <typename T, int P>
T item(size_t s);
into something like that:
template<int P>
T item<typename T>(size_t s)
{
//for all numeric types
return static_cast<T>(rand());
}
template <int P>
string item<string>(size_t s)
{
return "asdf";
}
Both are going to require partial specialization. The first is going to require SFINAE. Since function templates can only be fully specialized you’ll have to use a helper object of some sort.
Alternatively you might consider tag dispatching:
Or you could mix some metafunction classes up to match with tags and use a vector of pairs…iterate through them testing each first (the metafunction class) and returning the second (the tag) to pick an implementation.
There’s lots and lots and lots of ways to go about it.