Thread: template keyword needed

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    template keyword needed

    Hello everyone,


    In the Bjarne's book, there is a line of code like this,

    Code:
    typedef typename A::template rebind <Link>::other Link_alloc;
    It means if A is allocator, we can use use to allocate arbitrary type of object instance, said by Bjarne.

    I understand this statement, my question is about the grammar. Do we need the template keyword? Why?

    I have tested without temlpate keyword, the code still works. Any ideas?

    Code:
    template <class T1> class  Foo {
    public:
    	template <class T2> class Goo {
    	public:
    		typedef T2	GooValueType;
    	};
    };
    
    int main()
    {
    	Foo <int>::Goo<char>::GooValueType a1;
    	Foo <int>::template Goo<char>::GooValueType a2;
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by George2 View Post
    I understand this statement, my question is about the grammar. Do we need the template keyword? Why?
    You need the template keyword. The reason for this is that "rebind" is a template.

    I have tested without temlpate keyword, the code still works. Any ideas?
    You are using an older compiler which doesn't care.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi brewbuck,


    1.

    I am not using old compiler, but new Visual Studio 2008.

    2.

    To use template class/struct, when do we need to use template keyword, when do not need? I am confused. For example, deque is a template class, but we do not need the template keyword to use it, for example,

    deque<int>::iterator_type other than template deque<int>::iterator_type

    Quote Originally Posted by brewbuck View Post
    You need the template keyword. The reason for this is that "rebind" is a template.



    You are using an older compiler which doesn't care.

    regards,
    George

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    s/old/stupid/
    I thought the recent assigning to reference thread would have taught you to disable language extensions before trying things out.

    You need the template keyword under the same circumstances that you need the typename keyword: when addressing a dependent name.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Unfortunately, disabling language extensions fails to compile most projects, since Microsoft's headers are full of language extensions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    Quote Originally Posted by CornedBee View Post

    You need the template keyword under the same circumstances that you need the typename keyword: when addressing a dependent name.
    You mean when there is typename/class, we must use template at the same time. If there is no typename/class, there is no need to use template?


    regards,
    George

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, no, no.

    Ah, you haven't spent much time writing templates yet, have you? You really should do that before trying to understand dependent types.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    I think you mean using typename inside <> together with template keyword, right? Please look at the original code I quoted from Bjarne's book, typename is not in <>.

    Code:
    typedef typename A::template rebind <Link>::other Link_alloc;
    I have tried the following code also compiles,

    Code:
    typedef typename A::rebind <Link>::other Link_alloc;
    If I do not fully understand your points or my above code (see latter case) violates any C++ Spec, please feel free to let me know.

    Quote Originally Posted by CornedBee View Post
    No, no, no.

    Ah, you haven't spent much time writing templates yet, have you? You really should do that before trying to understand dependent types.

    regards,
    George

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think you mean using typename inside <> together with template keyword, right?
    No, I don't.

    I have tried the following code also compiles,
    On your compiler. But it's not conformant.

    http://womble.decadentplace.org.uk/c...disambiguation
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's funny how you mention it's not conforming when the compiler is actually smarter than a compiler needs to be
    Last edited by Elysia; 03-04-2008 at 07:18 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I was actually referring to the code being not conforming. (Just realized "conformant" isn't a word.) The compiler is conforming, as long as it accepts the correct code, too.

    I just don't think this extension is helpful, because other compilers aren't this smart. Thus, using the extension gives you non-portable code at minimal benefit (when you're using templates, correct placement of typename and template is the least of your problems). The way I see it, it's just an attempt at vendor lock-in.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    Quote Originally Posted by CornedBee View Post
    No, I don't.


    On your compiler. But it's not conformant.

    http://womble.decadentplace.org.uk/c...disambiguation
    I have made some study on this. Here is something I found confused.

    --------------------
    What are dependent names?

    A: Dependent names are names whose definitions are considered to depend upon the template parameters and for which there is no declaration within the template definition. They are resolved only when the template is instantiated. Those that are intended to refer to types or templates may require disambiguation.

    If the resolution of a dependent function name uses argument-dependent lookup, declarations in the arguments' namespaces that are visible at the point of instantiation will be considered as well as declarations visible at the point of definition. (The former is normally a superset of the latter, but may not be.)
    --------------------

    Two confusions,

    1. What means " for which there is no declaration within the template definition"?

    2. What means "declarations in the arguments' namespaces that are visible at the point of instantiation will be considered as well as declarations visible at the point of definition."?

    Could you show some pseudo code please?

    BTW: I feel I lack some knowledge of template programming in-depth even if I use STL template everyday. I tried ti read appendix C for Bjarne's book, and it contains more confusing terms about dependent names (e.g. section C.13.8.1).


    regards,
    George

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Daveed Vandevoorde, Nicolai Josuttis
    "C++ Templates"
    Addison Wesley

    Buy and read.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee,


    I have got the book. Which chapter do you mean most relevant?

    Quote Originally Posted by CornedBee View Post
    Daveed Vandevoorde, Nicolai Josuttis
    "C++ Templates"
    Addison Wesley

    Buy and read.

    regards,
    George

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    All of them.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM