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

  • Home
  • SEARCH
  • 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 8502749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:34:20+00:00 2026-06-11T01:34:20+00:00

Do all method declarations in a class that has a template parameter need to

  • 0

Do all method declarations in a class that has a template parameter need to be declared with that parameter? The compiler seems to want something like the following

// Queue.h
template <class ItemType>
class Queue
{
    Queue();
    ItemType dequeue();
    int count();
    /* ... other methods ... */
};
// Queue.cpp
template <class ItemType>
Queue<ItemType>::Queue()
{ /*...*/ }
template <class ItemType>
int Queue<ItemType>::count()
{/*...*/}
template <class ItemType>
ItemType Queue<ItemType>::dequeue()
{/* ... */}

Coming from Java/C#, this seems a bit redundant–I feel like I should be able to define the methods more like this:

int Queue::count() 

The signature does not reference ItemType, so we can omit it.

Queue::ItemType Queue::dequeue() 

The signature references ItemType, but the compiler knows that we’re talking about the template parameter because we qualify the identifier with Queue::

  • 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-11T01:34:21+00:00Added an answer on June 11, 2026 at 1:34 am

    Yes, you need to provide the template argument. Note that while it might look redundant, it is not. C++ templates are a much more powerful tool that Java generics, and they allow for specializations. That means that with a base template Queue there might be multiple implementations that match different template arguments and have different definitions. Alternatively, you can have a single Queue template with multiple specializations for some functions. These two situations require you to provide both the template argument list an the class template arguments:

    // Just member specialization
    template <typename T>
    struct X {
       void foo() { std::cout << "generic\n"; }
    };
    // specialize just this member for `int`
    template <>
    void X<int>::foo() { std::cout << "int\n"; }
    int main() {
       X<double> xd; xd.foo();  // generic
       X<int>    xi; xi.foo();  // int
    }
    
    // Class template specialization
    template <typename T>
    struct X {
       void foo();
    }
    template <typename T>
    struct X<T*> {
       void bar();
    }
    template <typename T>
    void X<T>::foo() { std::cout << "generic\n"; }
    template <typename T>
    void X<T*>::bar() { std::cout << "ptr\n"; }
    int main() {
       X<int > xi; xi.foo();       // generic
       X<int*> xp; xp.bar();       // ptr
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to intercept the calls to all the method calls to an Interface.
Is it possible to call a class method of all objects in a list?
I understand that a declaration of a delegate is something like this: public delegate
I'm writing a method in C# (2.0) that has to return a collection of
My assignment is to make a class that acts like a standard library List
Suppose I want to have a private method of a class to be visible
I have a base class with a static factory method that makes derived classes:
The All method is supposed to evaluate the argument against all elements in the
Do you know how to trace all method invocations in an .NET application. My
I am writing a catch all method for my controller for ajax. It is

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.