Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specializating a function template that takes a universal reference parameter

How do I specialize a function template 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.

like image 296
David Stone Avatar asked Nov 30 '12 22:11

David Stone


2 Answers

The best solution in my opinion is to use a tag dispatch system where you overload on tags rather than the actual type:

struct foo {
    struct tag {};
};

struct bar {
    struct tag {};
};

template<typename Destination, typename Source>
Destination server_cast(Source && source, foo::tag) {
    // foo
}

template<typename Destination, typename Source>
Destination server_cast(Source && source, bar::tag) {
    // bar
}

template<typename Destination, typename Source>
Destination server_cast(Source && source) {
    return server_cast<Destination>(std::forward<Source>(source), typename std::remove_reference<Source>::type::tag());
}
like image 63
Pubby Avatar answered Nov 19 '22 07:11

Pubby


The most extensible thing to do is to create a template class specialization.

template< class X > struct Whatever {
    void f(){ ... }
};

template<> struct Whatever<UserType> {
    void f(){ ... }
};

The reason I say this is the most extensible is because you can add a specialization anywhere, inside or outside the file that defines Whatever.

This is complimentary, not exclusive, to the tag dispatch solution suggested by Pubby.

like image 29
SplinterOfChaos Avatar answered Nov 19 '22 08:11

SplinterOfChaos