Specializating a function template that takes a universal reference parameter
Asked Answered
A

2

10

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.

Acherman answered 30/11, 2012 at 22:27 Comment(3)
Should f be foo? Also, what is the context of the problem? You generally want overloads, not specializations.Stringency
@Stringency Perhaps I used the wrong terminology. I have edited my question with more detail on what I am trying to accomplishAcherman
For a really nice writeup on this issue, see: mortoray.com/2013/06/03/…Analogy
H
2

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());
}
Hemipterous answered 30/11, 2012 at 22:48 Comment(1)
I'll have to think about this some more.Acherman
P
1

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.

Pol answered 30/11, 2012 at 23:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.