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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:44:57+00:00 2026-05-11T09:44:57+00:00

I am trying to understand some C++ syntax: template<class T> class Foo { Foo();

  • 0

I am trying to understand some C++ syntax:

template<class T> class Foo  {    Foo();     template<class U>    Foo(const Foo<U>& other); };  template<class T> Foo<T>::Foo() { /*normal init*/ }  template<class T> template<class U> Foo<T>::Foo(const Foo<U>& other) { /*odd copy constructed Foo*/ } 

So, I wrote code like this, and it happens to compile fine in windows and linux. What I don’t understand is why the copy constructor has two templates defined as so. Basically, I had to expirment a bit before I found the correct syntax and I would like to know why that particular syntax is correct, and not something like template<class T, class U>.

  • 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. 2026-05-11T09:44:58+00:00Added an answer on May 11, 2026 at 9:44 am

    It has to have separate template clauses for each template that is involved. Here, two templates are involved, that all deserve their (non-empty) template clauses:

    • The class template Foo
    • The constructor template

    Consider this case which fails because of the ambiguity as to where the parameter U belongs to

    template<typename T> struct A {     template<typename U> void f(); };  template<typename T, typename U> void A<T>::f() { } 

    Now, what is up with the parameter U? Sure the compiler could guess it could belong to f, but guesswork is not what the compiler likes 🙂 The existing rule says that depending on the nesting of templates, template clauses appear in the right order. Everything is clear then.

    Even if one comes up with a rule how to match the parameters to arguments of the templates involved (so far i don’t see a real difficulty in doing that), it would be inconsistent. Because as of now, one template clause lists all parameters that the corresponding template accepts. Much like a function parameter list. If we would put everything into one clause, that clear semantic could be broken – not to mention that when we put the definition into the class again, all of a sudden the template would get its own clause:

    // provides arguments for A's parameters, then for f ones  // when it's called A<int> a;  a.f<bool>(); 

    It’s much more natural when we have separate template clauses that catch each their own arguments. So, the syntax for the above wrong definition is

    template<typename T>   template<typename U> void A<T>::f() { } 

    Now, also the reader of the code immediately sees that this is a definition of a member template, and not a (potential accidentally declared but unused) second parameter for A.

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

Sidebar

Ask A Question

Stats

  • Questions 109k
  • Answers 109k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use freopen to redirect to dev/null: freopen("/dev/null", "w", stderr); May 11, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer Have you tried redirecting stderr to /dev/null ? 2>/dev/null So… May 11, 2026 at 9:24 pm
  • Editorial Team
    Editorial Team added an answer Here's a short script that defines and uses a private… May 11, 2026 at 9:24 pm

Related Questions

I've been talking with friends and some completely agree that templates in C++ should
I need some mind reading here, since I am trying to do what I
Note The question below was asked in 2008 about some code from 2003. As
I am trying to understand some assembly. The assembly as follows, I am interested

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.