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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:24:45+00:00 2026-05-15T19:24:45+00:00

I’ve been trying to work with templates for a while now and the more

  • 0

I’ve been trying to work with templates for a while now and the more I do the less I realise I understand. This latest problem feels like it has unearthed a rather fundamental misunderstanding on my part and I’m starting to think more than ever that, “Right, tomorrow I shouldn’t write any code but instead find a library with a good CS section and just read everything they have on templates”! I wonder if in the mean time you can help me.

So, the following code,

template <typename T>    // or replace `typename` with `class`
struct Foo {
    struct Bar {};
    Foo(Bar) {}
};

Foo<float>::Bar x;
Foo<int> y (x);

doesn’t compile since x is of type Foo<float>::Bar but to construct y we need a Foo<int>::Bar. That’s fine, and expected, but now consider the following,

template <int I>
struct Foo {
    struct Bar {};
    Foo(Bar) {}
};

Foo<0>::Bar x;
Foo<1> y (x);

I was hoping/thinking (although, thankfully, not as yet relying on) that x would be of type Foo<0>::Bar and to construct y we would need a Foo<1>::Bar and as such it would not compile – as in the previous example. But it seems that both are in fact of type Foo<int>::Bar and so, this will compile.

So, I wonder, what, first, is the correct terminology to describe this difference between a typename/class parameterized template and an integral type parameterized one, what other differences in behaviour does this entail, and, what method could I use to solve this problem and get the desired behaviour for this simple example so that Foo<0> and Foo<1> will describe incompatible types?

And, before that trip to the library, any links to “essential”, online, reading materials on the subject would be welcomed too.
Thanks.

  • 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-15T19:24:46+00:00Added an answer on May 15, 2026 at 7:24 pm

    On gcc 4.4.3 your second example fails to compile with the message “error: no matching function for call to ‘Foo<1>::Foo(Foo<0>::Bar&)'”, which is exactly what you expected to happen.

    So you did not misunderstand anything. If this compiles for you, that’s non-standard behavior by your compiler.

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

Sidebar

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.