Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8918579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:47:30+00:00 2026-06-15T05:47:30+00:00

How do I specialize a function template that takes a universal reference parameter? foo.hpp:

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T05:47:31+00:00Added an answer on June 15, 2026 at 5:47 am

    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());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to specialize following member function: class foo { template<typename T> T get()
I want to specialize a class template with the following function: template <typename T>
Say I have a templated class: template <typename T> class foo { void do_someting(T
I had a wrong perception that template function in a class is instantiated only
Given a class declaration class A { template <typename T> T foo(); }; I
I have a (free) function template that looks like this template <typename T> T
I have a sample class where I need to specialize the Print function if
In C++, I am trying to specialize a templated function for a object that
I've been trying to implement a function that needs partial template specializations and fallen
How can I specialize or overload function func so that specialization handles all instances

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.