Why isn't `typename` required for a base class that is a nested type?Where and why do I have to put the “template” and “typename” keywords?Difference of keywords 'typename' and 'class' in templates?C++ typename and inner classesPretty-print C++ STL containersImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionDifferent return type for same method of a base classWhy not inherit from List<T>?required by substitution of template<class Stepper>

Hough transform algorithm - Idiomatic c++

Why should I invest so much in 401(k)?

My PhD defense is next week and I am having negative thoughts about my work and knowledge. Any advice on how to tackle this?

How can player characters, creatures and NPCs heal plants?

Triangular domino tiling of an almost regular hexagon

If a photon truly goes through both slits (at the same time), then why can't we detect it at both slits (at the same time)?

Vertex Size for more than one vertex

Is it acceptable for the secretary to have full access to our entire Outlook agenda?

Which cohomology classes are detected by tori?

What are these color strips for?

Is it possible to protect saved games on Nintendo Switch from being played by other users?

What antenna is this in an Apollo 15 LM photo?

Are silicone socks safe?

Sent technical test that was impossible, other candidates completed it

What is the difference between chemical equilibrium and dynamic equilibrium?

Patent Agreement in Order to Graduate

What type of interpreter were most 8-bit BASIC implementations?

Transferring $ from LLC to Personal account

C - wrapping globals in a struct?

Prevent function taking const std::string& from accepting 0

Get a character at a specified location

Are there any proofs of exponnential lower bound time complexity

How can a bigfoot hide from satellites?

How would a medieval village protect themselves against dinosaurs?



Why isn't `typename` required for a base class that is a nested type?


Where and why do I have to put the “template” and “typename” keywords?Difference of keywords 'typename' and 'class' in templates?C++ typename and inner classesPretty-print C++ STL containersImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionDifferent return type for same method of a base classWhy not inherit from List<T>?required by substitution of template<class Stepper>






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









25


















I've been very surprised to see that it is not necessary to add typename when a dependent type appears as a base class:



struct B ;

struct wr
typedef B type; ;

template<class T>
struct A : T::type
;

int main()

A<wr> a;
(void)a;



Why isn't typename required in front of T::type?










share|improve this question

































    25


















    I've been very surprised to see that it is not necessary to add typename when a dependent type appears as a base class:



    struct B ;

    struct wr
    typedef B type; ;

    template<class T>
    struct A : T::type
    ;

    int main()

    A<wr> a;
    (void)a;



    Why isn't typename required in front of T::type?










    share|improve this question





























      25













      25









      25


      1






      I've been very surprised to see that it is not necessary to add typename when a dependent type appears as a base class:



      struct B ;

      struct wr
      typedef B type; ;

      template<class T>
      struct A : T::type
      ;

      int main()

      A<wr> a;
      (void)a;



      Why isn't typename required in front of T::type?










      share|improve this question
















      I've been very surprised to see that it is not necessary to add typename when a dependent type appears as a base class:



      struct B ;

      struct wr
      typedef B type; ;

      template<class T>
      struct A : T::type
      ;

      int main()

      A<wr> a;
      (void)a;



      Why isn't typename required in front of T::type?







      c++ templates inheritance






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 11 at 21:35









      bubbleking

      1,9811 gold badge17 silver badges32 bronze badges




      1,9811 gold badge17 silver badges32 bronze badges










      asked Sep 11 at 7:07









      Peregring-lkPeregring-lk

      7,6575 gold badges31 silver badges69 bronze badges




      7,6575 gold badges31 silver badges69 bronze badges

























          3 Answers
          3






          active

          oldest

          votes


















          13



















          It's a special case, as others noted. To quote the standard on this:




          [temp.res]



          5 A qualified name used as the name in a class-or-decltype or an
          elaborated-type-specifier is implicitly assumed to name a type,
          without the use of the typename keyword. In a nested-name-specifier
          that immediately contains a nested-name-specifier that depends on a
          template parameter, the identifier or simple-template-id is implicitly
          assumed to name a type, without the use of the typename keyword.
          [ Note: The typename keyword is not permitted by the syntax of these
          constructs.  — end note ]




          And come C++20, there will be be even more exceptions to the need for typename.






          share|improve this answer


























          • What other exceptions will there be?

            – 0x499602D2
            Sep 11 at 16:39











          • @0x499602D2 - Function return types, type arguments to the various *_cast operators and default template arguments to name a couple. The paper has examples and exact details.

            – StoryTeller - Unslander Monica
            Sep 11 at 19:08



















          19




















          Why isn't typename required in front of T::type?




          Because you cannot inherit from a value. You use typename to tell the compiler that a given nested identifier is a type, but for inheritance, that must be the case anyhow so you can omit it - that's why the language provides an exception to the typename- rule for base-specifiers. From cppreference (emphasis mine):




          The typename disambiguator for dependent names



          In a declaration or a definition of a template, including alias template, a name that is not a member of the current instantiation and is dependent on a template parameter is not considered to be a type unless the keyword typename is used or unless it was already established as a type name, e.g. with a typedef declaration or by being used to name a base class.




          Note that we will get more places where typename can be omitted, see P0634.






          share|improve this answer



































            5



















            You only need to use typename if you need to tell the compiler to expect a type rather than something else.



            Since only a type can be inherited from, there is no ambiguity, and so typename is superfluous.






            share|improve this answer



























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );














              draft saved

              draft discarded
















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57883831%2fwhy-isnt-typename-required-for-a-base-class-that-is-a-nested-type%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown


























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              13



















              It's a special case, as others noted. To quote the standard on this:




              [temp.res]



              5 A qualified name used as the name in a class-or-decltype or an
              elaborated-type-specifier is implicitly assumed to name a type,
              without the use of the typename keyword. In a nested-name-specifier
              that immediately contains a nested-name-specifier that depends on a
              template parameter, the identifier or simple-template-id is implicitly
              assumed to name a type, without the use of the typename keyword.
              [ Note: The typename keyword is not permitted by the syntax of these
              constructs.  — end note ]




              And come C++20, there will be be even more exceptions to the need for typename.






              share|improve this answer


























              • What other exceptions will there be?

                – 0x499602D2
                Sep 11 at 16:39











              • @0x499602D2 - Function return types, type arguments to the various *_cast operators and default template arguments to name a couple. The paper has examples and exact details.

                – StoryTeller - Unslander Monica
                Sep 11 at 19:08
















              13



















              It's a special case, as others noted. To quote the standard on this:




              [temp.res]



              5 A qualified name used as the name in a class-or-decltype or an
              elaborated-type-specifier is implicitly assumed to name a type,
              without the use of the typename keyword. In a nested-name-specifier
              that immediately contains a nested-name-specifier that depends on a
              template parameter, the identifier or simple-template-id is implicitly
              assumed to name a type, without the use of the typename keyword.
              [ Note: The typename keyword is not permitted by the syntax of these
              constructs.  — end note ]




              And come C++20, there will be be even more exceptions to the need for typename.






              share|improve this answer


























              • What other exceptions will there be?

                – 0x499602D2
                Sep 11 at 16:39











              • @0x499602D2 - Function return types, type arguments to the various *_cast operators and default template arguments to name a couple. The paper has examples and exact details.

                – StoryTeller - Unslander Monica
                Sep 11 at 19:08














              13















              13











              13









              It's a special case, as others noted. To quote the standard on this:




              [temp.res]



              5 A qualified name used as the name in a class-or-decltype or an
              elaborated-type-specifier is implicitly assumed to name a type,
              without the use of the typename keyword. In a nested-name-specifier
              that immediately contains a nested-name-specifier that depends on a
              template parameter, the identifier or simple-template-id is implicitly
              assumed to name a type, without the use of the typename keyword.
              [ Note: The typename keyword is not permitted by the syntax of these
              constructs.  — end note ]




              And come C++20, there will be be even more exceptions to the need for typename.






              share|improve this answer














              It's a special case, as others noted. To quote the standard on this:




              [temp.res]



              5 A qualified name used as the name in a class-or-decltype or an
              elaborated-type-specifier is implicitly assumed to name a type,
              without the use of the typename keyword. In a nested-name-specifier
              that immediately contains a nested-name-specifier that depends on a
              template parameter, the identifier or simple-template-id is implicitly
              assumed to name a type, without the use of the typename keyword.
              [ Note: The typename keyword is not permitted by the syntax of these
              constructs.  — end note ]




              And come C++20, there will be be even more exceptions to the need for typename.







              share|improve this answer













              share|improve this answer




              share|improve this answer










              answered Sep 11 at 7:20









              StoryTeller - Unslander MonicaStoryTeller - Unslander Monica

              126k19 gold badges273 silver badges338 bronze badges




              126k19 gold badges273 silver badges338 bronze badges















              • What other exceptions will there be?

                – 0x499602D2
                Sep 11 at 16:39











              • @0x499602D2 - Function return types, type arguments to the various *_cast operators and default template arguments to name a couple. The paper has examples and exact details.

                – StoryTeller - Unslander Monica
                Sep 11 at 19:08


















              • What other exceptions will there be?

                – 0x499602D2
                Sep 11 at 16:39











              • @0x499602D2 - Function return types, type arguments to the various *_cast operators and default template arguments to name a couple. The paper has examples and exact details.

                – StoryTeller - Unslander Monica
                Sep 11 at 19:08

















              What other exceptions will there be?

              – 0x499602D2
              Sep 11 at 16:39





              What other exceptions will there be?

              – 0x499602D2
              Sep 11 at 16:39













              @0x499602D2 - Function return types, type arguments to the various *_cast operators and default template arguments to name a couple. The paper has examples and exact details.

              – StoryTeller - Unslander Monica
              Sep 11 at 19:08






              @0x499602D2 - Function return types, type arguments to the various *_cast operators and default template arguments to name a couple. The paper has examples and exact details.

              – StoryTeller - Unslander Monica
              Sep 11 at 19:08














              19




















              Why isn't typename required in front of T::type?




              Because you cannot inherit from a value. You use typename to tell the compiler that a given nested identifier is a type, but for inheritance, that must be the case anyhow so you can omit it - that's why the language provides an exception to the typename- rule for base-specifiers. From cppreference (emphasis mine):




              The typename disambiguator for dependent names



              In a declaration or a definition of a template, including alias template, a name that is not a member of the current instantiation and is dependent on a template parameter is not considered to be a type unless the keyword typename is used or unless it was already established as a type name, e.g. with a typedef declaration or by being used to name a base class.




              Note that we will get more places where typename can be omitted, see P0634.






              share|improve this answer
































                19




















                Why isn't typename required in front of T::type?




                Because you cannot inherit from a value. You use typename to tell the compiler that a given nested identifier is a type, but for inheritance, that must be the case anyhow so you can omit it - that's why the language provides an exception to the typename- rule for base-specifiers. From cppreference (emphasis mine):




                The typename disambiguator for dependent names



                In a declaration or a definition of a template, including alias template, a name that is not a member of the current instantiation and is dependent on a template parameter is not considered to be a type unless the keyword typename is used or unless it was already established as a type name, e.g. with a typedef declaration or by being used to name a base class.




                Note that we will get more places where typename can be omitted, see P0634.






                share|improve this answer






























                  19















                  19











                  19










                  Why isn't typename required in front of T::type?




                  Because you cannot inherit from a value. You use typename to tell the compiler that a given nested identifier is a type, but for inheritance, that must be the case anyhow so you can omit it - that's why the language provides an exception to the typename- rule for base-specifiers. From cppreference (emphasis mine):




                  The typename disambiguator for dependent names



                  In a declaration or a definition of a template, including alias template, a name that is not a member of the current instantiation and is dependent on a template parameter is not considered to be a type unless the keyword typename is used or unless it was already established as a type name, e.g. with a typedef declaration or by being used to name a base class.




                  Note that we will get more places where typename can be omitted, see P0634.






                  share|improve this answer

















                  Why isn't typename required in front of T::type?




                  Because you cannot inherit from a value. You use typename to tell the compiler that a given nested identifier is a type, but for inheritance, that must be the case anyhow so you can omit it - that's why the language provides an exception to the typename- rule for base-specifiers. From cppreference (emphasis mine):




                  The typename disambiguator for dependent names



                  In a declaration or a definition of a template, including alias template, a name that is not a member of the current instantiation and is dependent on a template parameter is not considered to be a type unless the keyword typename is used or unless it was already established as a type name, e.g. with a typedef declaration or by being used to name a base class.




                  Note that we will get more places where typename can be omitted, see P0634.







                  share|improve this answer















                  share|improve this answer




                  share|improve this answer








                  edited Sep 11 at 8:29

























                  answered Sep 11 at 7:09









                  lubgrlubgr

                  29.1k3 gold badges43 silver badges88 bronze badges




                  29.1k3 gold badges43 silver badges88 bronze badges
























                      5



















                      You only need to use typename if you need to tell the compiler to expect a type rather than something else.



                      Since only a type can be inherited from, there is no ambiguity, and so typename is superfluous.






                      share|improve this answer






























                        5



















                        You only need to use typename if you need to tell the compiler to expect a type rather than something else.



                        Since only a type can be inherited from, there is no ambiguity, and so typename is superfluous.






                        share|improve this answer




























                          5















                          5











                          5









                          You only need to use typename if you need to tell the compiler to expect a type rather than something else.



                          Since only a type can be inherited from, there is no ambiguity, and so typename is superfluous.






                          share|improve this answer














                          You only need to use typename if you need to tell the compiler to expect a type rather than something else.



                          Since only a type can be inherited from, there is no ambiguity, and so typename is superfluous.







                          share|improve this answer













                          share|improve this answer




                          share|improve this answer










                          answered Sep 11 at 7:10









                          BathshebaBathsheba

                          198k29 gold badges295 silver badges415 bronze badges




                          198k29 gold badges295 silver badges415 bronze badges































                              draft saved

                              draft discarded















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f57883831%2fwhy-isnt-typename-required-for-a-base-class-that-is-a-nested-type%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown









                              Popular posts from this blog

                              Tamil (spriik) Luke uk diar | Nawigatjuun

                              Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                              Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?