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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:05:59+00:00 2026-05-27T15:05:59+00:00

class A { static int iterator; class iterator { […] }; […] }; I

  • 0
class A
{
   static int iterator;
   class iterator
   {
      [...]
   };
   [...]
};

I (think I) understand the reason why typename is needed here:

template <class T>
void foo() {
   typename T::iterator* iter;
   [...]
}

but I don’t understand the reason why typename is not needed here:

void foo() {
   A::iterator* iter;
   [...]
}

Can anyone explain?


EDIT:

The reason why the compiler does not have a problem with the latter, I found to be answered well in a comment:

in the case of A::iterator I don’t see why the compiler wouldn’t confuse it with the static int iterator ? – xcrypt

@xcrypt because it knows what both A::iterators are and can pick which one depending on how it is used – Seth Carnegie


The reason why the compiler needs typename before the qualified dependent names, is in my opinion answered very well in the accepted answer by Kerrek SB. Be sure to also read the comments on that answer, especially this one by iammilind:

“T::A * x;, this expression can be true for both cases where T::A is a type and T::A is a value. If A is a type, then it will result in pointer declaration; if A is a value, then it will result in multiplication. Thus a single template will have different meaning for 2 different types, which is not acceptable.”

  • 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-27T15:05:59+00:00Added an answer on May 27, 2026 at 3:05 pm

    A name in C++ can pertain to three different tiers of entities: Types, values, and templates.

    struct Foo
    {
        typedef int A;                   // type
        static double B;                 // value
        template <typename T> struct C;  // template
    };
    

    The three names Foo::A, Foo::B and Foo::C are examples of all three different tiers.

    In the above example, Foo is a complete type, and so the compiler knows already what Foo::A etc. refer to. But now imagine this:

    template <typename T> struct Bar
    {
        T::A x;
    };
    

    Now we are in trouble: what is T::A? if T = Foo, then T::A = int, which is a type, and all is well. But when T = struct { static char A; };, then T::A is a value, which doesn’t make sense.

    Therefore, the compiler demands that you tell it what T::A and T::B and T::C are supposed to be. If you say nothing, it is assumed to be a value. If you say typename, it is a typename, and if you say template, it is a template:

    template <typename T> struct Bar
    {
        typename T::A x;    // ah, good, decreed typename
    
        void foo()
        {
            int a = T::B;   // assumed value, OK
    
            T::template C<int> z;  // decreed template
            z.gobble(a * x);
        }
    };
    

    Secondary checks such as whether T::B is convertible to int, whether a and x can be multiplied, and whether C<int> really has a member function gobble are all postponed until you actually instantiate the template. But the specification whether a name denotes a value, a type or a template is fundamental to the syntactic correctness of the code and must be provided right there during the template definition.

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

Sidebar

Related Questions

Here's the code: class qual { public static int fibonacci(int n) { if (n
class MyClass { public: void method2() { static int i; ... } }; Will
class a { int variable;//4 bytes } class a { static int variable;//? bytes
Which is appropriate: class xyz { static int xyzOp1() { } static int xyzOp2()
If I have something like class Base { static int staticVar; } class DerivedA
<%! class father { static int s = 0; } %> <% father f1
I have this user defined function. public partial class UserDefinedFunctions { static int i;
say I have: class Test { public static int Hello = 5; } This
Let's say we have these two classes: public class Base { public static int
Suppose I have following code package memoryleak; public class MemoryLeak { public static int

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.