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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:24:18+00:00 2026-05-26T10:24:18+00:00

Possible Duplicate: Officially, what is typename for? Where and why do I have to

  • 0

Possible Duplicate:
Officially, what is typename for?
Where and why do I have to put the template and typename keywords?

consider the code below:

template<class K>
class C {
    struct P {};
    vector<P> vec;
    void f();
};

template<class K> void C<K>::f() {
    typename vector<P>::iterator p = vec.begin();
}

Why is the “typename” keyword necessary in this example?
Are there any other cases where “typename” must be specified?

  • 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-26T10:24:19+00:00Added an answer on May 26, 2026 at 10:24 am

    Short answer: Whenever referring to a nested name that is a dependent name, i.e. nested inside a template instance with unknown parameter.

    Long answer: There are three tiers of entities in C++: values, types, and templates. All of those can have names, and the name alone doesn’t tell you which tier of entity it is. Rather, the information about the nature of a name’s entity must be inferred from the context.

    Whenever this inference is impossible, you have to specify it:

    template <typename> struct Magic; // defined somewhere else
    
    template <typename T> struct A
    {
      static const int value = Magic<T>::gnarl; // assumed "value"
    
      typedef typename Magic<T>::brugh my_type; // decreed "type"
      //      ^^^^^^^^
    
      void foo() {
        Magic<T>::template kwpq<T>(1, 'a', .5); // decreed "template"
        //        ^^^^^^^^
      }
    };
    

    Here the names Magic<T>::gnarl, Magic<T>::brugh and Magic<T>::kwpq had to be expliciated, because it is impossible to tell: Since Magic is a template, the very nature of the type Magic<T> depends on T — there may be specializations which are entirely different from the primary template, for example.

    What makes Magic<T>::gnarl a dependent name is the fact that we’re inside a template definition, where T is unknown. Had we used Magic<int>, this would be different, since the compiler knows (you promise!) the full definition of Magic<int>.

    (If you want to test this yourself, here’s a sample definition of Magic that you can use. Pardon the use of constexpr in the specializaation for brevity; if you have an old compiler, feel free to change the static member constant declaration to the old-style pre-C++11 form.)

    template <typename T> struct Magic
    {
      static const T                    gnarl;
      typedef T &                       brugh;
      template <typename S> static void kwpq(int, char, double) { T x; }
    };
    template <> struct Magic<signed char>
    {
      // note that `gnarl` is absent
      static constexpr long double brugh = 0.25;  // `brugh` is now a value
      template <typename S> static int kwpq(int a, int b) { return a + b; }
    };
    

    Usage:

    int main()
    {
      A<int> a;
      a.foo();
    
      return Magic<signed char>::kwpq<float>(2, 3);  // no disambiguation here!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Where and why do I have to put “template” and “typename” on
Possible Duplicate: Officially, what is typename for? When I use template <typename TMap> typename
Possible Duplicate: Passing a Int value to another class I have been trying to
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: How to successfully rewrite old mysql-php code with deprecated mysql_* functions? I
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: How to call a JavaScript function from PHP? I have a php

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.