Templated templates...

This is a discussion on Templated templates... within the C++ Programming forums, part of the General Programming Boards category; ...my temple hurts. I'm writing (potentially) template-heavy code, and I've encountered this problem which fortunately can be isolated: Code: #include ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    725

    Templated templates...

    ...my temple hurts.

    I'm writing (potentially) template-heavy code, and I've encountered this problem which fortunately can be isolated:

    Code:
    #include <vector>
    template <class a_type>
    class a_class
    {
        public:
            void foo()
            {
                std::vector <a_type>::iterator i;
            }
    };
    Now when I try to compile this code in GCC it gives
    Code:
    In member function 'void a_class<a_type>::foo()': expected ';' before "i"
    I'm thinking the reason is in the way the STL implements its iterators... you need a well-defined class for them. But I'm not sure.

    EDIT: Okay, after fiddling around I found that using typename would resolve the error. (And finally got a chance to learn what the keyword did.) I'm still open to explanations...
    Last edited by jafet; 09-03-2006 at 02:36 AM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,620
    Yup, I was just about to post and say "use the typename keyword" when I saw your EDIT.

    The basic reason is that, when compiling code for a template, that the compiler has insufficient information to work out whether
    Code:
       std::vector<a_type>::iterator i;
    is dealing with an expression (eg something that can be evaluated) or if it is a declaration (as you intend in this case). It may not be obvious, but a minor change makes even humans do a double take;
    Code:
       std::vector<a_type>::iterator *i;
    because (unless we know the vector class off by heart) we don't know if this is declaring a pointer named i, of if it is silently multiplying some quantity named std::vector<a_type>::iterator to a variable named i and discarding the result.


    In other words, the compiler has no means to know (until you actually instantiate your template with a particular type) what a_type is, so it does not look at the std::vector<> template, so it cannot deduce that std::vector<a_type>::iterator actually represents a type name rather than a part of an expression. An example of "part of an expression" is a data member within the std::vector<> template. Without the typename keyword, the compiler assumes that std::vector<a_type>::iterator is part of an expression, and therefore complains as yours has. The typename keyword tells the compiler that the next token (std::vector<a_type>::iterator) is a type name rather than part of an expression.

    Some compilers get this wrong, and mistakenly let the code through, but then the most common problem is for some form of very obscure linker error.
    Last edited by grumpy; 09-03-2006 at 03:19 AM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Learn something everyday, eh... thanks grumpy.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 03:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 10:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21