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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:21:54+00:00 2026-05-12T21:21:54+00:00

It’s hard to get a word for this. Sometimes I see a class like

  • 0

It’s hard to get a word for this. Sometimes I see a class like this:

template <typename T>
class Wrapper
{
public:
    Wrapper(const T& t) : t_(t) {}
    Wrapper(const Wrapper& w) : t_(w.t_) {}
private:
    T t_;
}

As far as I can tell this is legitimate code. However, why is the copy constructor allowed to accept a const Wrapper& without explicitly stating that it needs a const Wrapper<T>&. When else is the template type implied? Is it allowed to write the copy constructor this way if you don’t use an in-class definition?

  • 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-05-12T21:21:54+00:00Added an answer on May 12, 2026 at 9:21 pm

    It is explicitly specified by the language standard in 14.6.1/1:

    Within the scope of class template,
    when the name of the template is
    neither qualified nor followed by <,
    it is equivalent to the name of the
    template followed by the
    template-parameters enclosed in <>.

    This was re-worded (through the concept of “injected class name”) in the later versions of the standard, but the point is that this behavior is explicitly spelled out in the document.

    To answer the second part of your question, this rule also applies to parameter declarations when writing out-of-class method definitions, but it doesn’t apply to the return type declarations. For example, this code is OK

    template <typename T> struct S {
      S foo(S);
    };
    
    template <typename T> S<T> S<T>::foo(S s) {
      /* whatever */
    }
    

    but you can’t remove the <T> bit from the return type in the definition of the method. (And you can’t remove <T> from the qualified name of the method.)

    As for the constructior specifically: your should use the full name (with <T>) for the class, but you should not use <T> in the name of the constructor itself. So the shortest form for out-of-class definition in your case would be

    template <typename T> Wrapper<T>::Wrapper(const Wrapper& w) : t_(w.t_)
    {
    }
    

    Note, that you can’t add the <T> bit to the constructor name even if you want to

    template <typename T> Wrapper<T>::Wrapper<T>(const Wrapper& w)
                                             ^ ERROR !!!
    

    P.S. This last claim needs further research. Comeau Online compiler thinks it is an error, while GCC thinks it is OK. I’ll return to it later.

    P.P.S. The compiler in MSVC++ 2005 complains about the latter declaration with a warning

    warning C4812: obsolete declaration style: please use 'Wrapper<T>::Wrapper' instead
    

    Interesting…

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into

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.