Specializating a template function that takes a universal reference parameter
Posted
by
David Stone
on Stack Overflow
See other posts from Stack Overflow
or by David Stone
Published on 2012-11-30T22:27:06Z
Indexed on
2012/11/30
23:04 UTC
Read the original article
Hit count: 246
How do I specialize a template function that takes a universal reference parameter?
foo.hpp:
template<typename T>
void foo(T && t) // universal reference parameter
foo.cpp
template<>
void foo<Class>(Class && class) {
// do something complicated
}
Here, Class
is no longer a deduced type and thus is Class
exactly; it cannot possibly be Class &
, so reference collapsing rules will not help me here. I could perhaps create another specialization that takes a Class &
parameter (I'm not sure), but that implies duplicating all of the code contained within foo
for every possible combination of rvalue / lvalue references for all parameters, which is what universal references are supposed to avoid.
Is there some way to accomplish this?
To be more specific about my problem in case there is a better way to solve it:
I have a program that can connect to multiple game servers, and each server, for the most part, calls everything by the same name. However, they have slightly different versions for a few things. There are a few different categories that these things can be: a move, an item, etc. I have written a generic sort of "move string to move enum" set of functions for internal code to call, and my server interface code has similar functions. However, some servers have their own internal ID that they communicate with, some use strings, and some use both in different situations.
Now what I want to do is make this a little more generic.
I want to be able to call something like ServerNamespace::server_cast<Destination>(source)
. This would allow me to cast from a Move
to a std::string
or ServerMoveID
. Internally, I may need to make a copy (or move from) because some servers require that I keep a history of messages sent. Universal references seem to be the obvious solution to this problem.
The header file I'm thinking of right now would expose simply this:
namespace ServerNamespace {
template<typename Destination, typename Source>
Destination server_cast(Source && source);
}
And the implementation file would define all legal conversions as template specializations.
© Stack Overflow or respective owner