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 9215031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:06:36+00:00 2026-06-18T02:06:36+00:00

I have a template class (OutgoingPacket) with two different functions: void _prepare() { assert(false);

  • 0

I have a template class (OutgoingPacket) with two different functions:

void _prepare() {
    assert(false); // this should have been specialized and the native function never called.
}

template <typename Args, typename ... RemArgs>
void _prepare(Args&& args, RemArgs&& ... remArgs) {
    assert(false); // this should have been specialized and the native function never called.
}

I then define some specializations of the two outside of the class definition:

// no args
template <> void OutgoingPacket<PacketServerAck> ::_prepare();
template <> void OutgoingPacket<PacketSup>       ::_prepare();
template <> void OutgoingPacket<PacketWelcome>   ::_prepare();
template <> void OutgoingPacket<PacketServerPing>::_prepare();

// with args
template <> template <> void OutgoingPacket<PacketGTFO>::_prepare<std::string>(std::string&& message);
template <> template <> void OutgoingPacket<PacketPlayer>::_prepare<std::shared_ptr<User>>(std::shared_ptr<User>&& user);

Function calls to prepare without arguments work as expected, but calls to the overload with arguments call the base template; they trigger the assert.

Why does this happen?


Update: I just tried modifying the specialization definitions to include the reference with the same result:

template <> template <> void OutgoingPacket<PacketGTFO>::_prepare<std::string&&>(std::string&& message);
template <> template <> void OutgoingPacket<PacketPlayer>::_prepare<std::shared_ptr<User>&&>(std::shared_ptr<User>&& user);

On a side note, the reason I’m doing it this way is because I didn’t feel that the base class, OutgoingPacket, should be littered with all of these different versions of the prepare function. And I didn’t feel that subclassing was appropriate because the differences between different OutgoingPackets would be very small (~4 lines).

Essentially, the OutgoingPacket object is created with arbitrary arguments which it then forwards to the prepare function:

template<typename ... Args>
OutgoingPacket(Args&&... args) {
    _prepare(std::forward<Args>(args)...);
}

If this is bad practice, could I get some advice on design?

  • 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-18T02:06:37+00:00Added an answer on June 18, 2026 at 2:06 am

    The reason is most likely that you are invoking your functions with arguments that are not rvalues.

    Please notice, that the function parameters of your primary variadic template bind to both rvalues and lvalues: in other words, they are universal references (non-standard term by Scott Meyers), while the function parameters in your specialized template bind only to rvalues.

    // The parameters are universal references
    template <typename Args, typename ... RemArgs>
    void _prepare(Args&& args, RemArgs&& ... remArgs) 
    
    // The parameters are NOT universal references
    template <> template <> 
    void OutgoingPacket<PacketGTFO>::_prepare<std::string>(std::string&& message);
    

    See this article and this presentation for an explanation of how universal references work. The tricky part is that the && suffix does not always mean rvalue reference: it does not, for instance, in the case of your primary template, but it does in the case of your specialized template.

    Thus, if the input arguments of your function are lvalues (e.g. variables with names rather than the return value of a function call), they will bind to your primary template rather than to your specialized template, due to regular overload resolution rules.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class that should allow void as a template parameter. This
I have non-template class with a templatized constructor. This code compiles for me. But
I have a template class Foo that takes two (or more) template arguments. I
I have template class Array where the template type T should be either pointer
Suppose you have: template<class T> class A { template<class T1> void foo(const T1& t1)
I have a template class that looks something like this: template<class T> class C
I have a template class template <class T> class myClass { public: /* functions
I have a template class called Variable , with a specialized constructor for char*,
I have a template class, say: template<class T> class someClient { void someCallbackA() {foo_->onA();}
I have a template class which goes like this: class Template{ public $pageTitle =

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.