Thread: VC 7.0 and templates

  1. #1
    Mongush
    Guest

    VC 7.0 and templates

    Why this code doesn't comile on VC 7.0? (gcc compile it)
    The idea is that the Item class should have a handle to itself, but the handle type is known only by the Conteiner class (so if i'd like to chage a conteiner I dont need to chage Item)
    Is any wy to solve this?
    Code:
    #include <list>
    using namespace std;
    template <class R>
    class Item {
    public:
    	typedef typename R::I_handle handle;
    	void SetHandle(handle h) {a=h;}
    	handle GetHandle() {return a;}
    private:
    	handle a;
    };
    struct Item_type
    {
    	template <class R>
    	struct I_Wrapper {
    		typedef Item<R> Item;
    	};
    };
    template <class I=Item_type>
    class Container {
    public:
    	typedef Container<I> Self;
    	typedef typename I::I_Wrapper<Self>::Item Item;
    	typedef list<Item> Item_list;
    	typedef typename Item_list::iterator I_handle;
    	Item_list items;
    
    };
    
    int main()
    {
    
    	Container<> a;
    	Container<>::Item i;
    	Container<>::Item i1;
    	i.SetHandle(i1.GetHandle());
    	a.items.push_back(i);
    }

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    56

    Thumbs down

    i installed microsoft visual studio to but
    i deleted it (by hand) it compiled nothing

    {
    with dev-c++ 4.0
    it words

    http://<br /> <a href="http://www.b....com</a><br />

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The MS compiler is awful when it comes to compliance. You're better off using dev-c++ with minigw. They're free and actually work (though even minigw struggles with some templating)!

  4. #4
    Mongush
    Guest
    Originally posted by Polymorphic OOP
    The MS compiler is awful when it comes to compliance. You're better off using dev-c++ with minigw. They're free and actually work (though even minigw struggles with some templating)!
    The thing is I have to do it on VC 7.0 somehow. BTW it does compile on VC 7.1 beta, but I cant wait for relese, I need this code working now.

  5. #5
    Mongush
    Guest

    Lightbulb

    OK I got it solved.
    I've replaced MS STL with STLport, and it works.
    But I still dont undestand what was a problem.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    I know VC++ 6.0 does not handle template specialization correctly. Are you sure you just changed out the STL implementation to fix it and nothing else (that is strange indeed).

    gg

  7. #7
    Mongush
    Guest
    Originally posted by Codeplug
    I know VC++ 6.0 does not handle template specialization correctly. Are you sure you just changed out the STL implementation to fix it and nothing else (that is strange indeed).

    gg
    VC++ 6.0 Dosent compile this code at all it gives INTERNAL COMPILER ERROR I think becouse it dosent support template as template argument. But VC++ 7.0 does support. That's why I've got suprised. I don't know what can be wrong with STL. But fact is fact. I just have changed STL.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I've been having problems with the STL provided by Microsoft as well. Changing was probably a good idea.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  9. #9
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Dude, don't use that code. Its way to confusing for even me! You're probably trying to test your compiler but I bet you will never do something that complex in real-life. VC7.0 is a great compiler, never had any problems with templates with it yet (and I use them all the time).

  10. #10
    Mongush
    Guest
    Originally posted by Speedy5
    Dude, don't use that code. Its way to confusing for even me! You're probably trying to test your compiler but I bet you will never do something that complex in real-life. VC7.0 is a great compiler, never had any problems with templates with it yet (and I use them all the time).
    I'm using this code alredy
    And actualy it's not as complex as it looks like. To not be confused with a such code I would suggest to read somthing about generic programming on C++.
    And BTW VC 7.0 still doesn't support partial template specification which is too bad(fortunately 7.1 does support), it doesnt support export template, but it's OK nobody does and STL in VC 7.0 is far from the standart.

  11. #11
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Speedy5
    Dude, don't use that code. Its way to confusing for even me! You're probably trying to test your compiler but I bet you will never do something that complex in real-life. VC7.0 is a great compiler, never had any problems with templates with it yet (and I use them all the time).
    It's not that complex. MS visual c++ is an awful compiler in terms of compliance. I had a lot of problems with the ms compiler. Almost once a week I'd run into another problem with it -- Enough so that I've gone to minigw and devc++. If you're using templates and claim that you're not having problems with them in visual c++, then you're not using them to the extent that we use them. It chokes on templated member functions, even nested class definitions -- two things that I use in almost every project.

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    You are right. I don't use templates to your extent although I do use them A LOT like templated member functions (which do work fine), default templates and etc. Looking into the docs, the compiler tells you you can't do certain things with templates. Oh well that sux but those features are for limited use at least for me.

    I just don't understand your code.

    struct Item_type: template <class R> struct I_Wrapper
    template <class I=Item_type> class Container

    When expanded for Container<>::Item, this is what you get:
    X: Item <Item_Type::I_Wrapper <Container<Item_type> > >

    But then Container<Item_type>'s Item_type's I_Wrapper's R is not specified! Even so, it has to have an Item_list which is a list of Items which is X again. To evaluate the Item for the Item_list of X, it has to do it again and again. I sense an infinite loop in Container<> a! It has to evaluate Item_list over and over and over.

    So can anyone explain this code to me?
    Last edited by Speedy5; 03-10-2003 at 03:40 PM.

  13. #13
    Mongush
    Guest
    To expalin hos it works let me simplify the code:
    Code:
    #include <list>
    using namespace std;
    template <class R>
    class Item {
    public:
    	typedef typename R::I_handle handle;
    	void SetHandle(handle h) {a=h;}
    	handle GetHandle() {return a;}
    private:
    	handle a;
    	
    };
    
    template  < template < class > class I = Item >
    class Container {
    public:
    	typedef Container<I> Self;
    	typedef typename I<Self> Item;
    	typedef list<Item> Item_list;
    	typedef typename Item_list::iterator I_handle;
    	Item_list items;
    
    };
    
    int main()
    {
    
    	Container<> a;
    	Container<>::Item i;
    	Container<>::Item i1;
    	i.SetHandle(i1.GetHandle());
    	a.items.push_back(i);
    }
    So we get rid of Wrapper, but get a template as template param
    The type dependencies contain a cycle; the item type need a template argument that tells it the handle type. The Container knows the handle type and can be used as the actual type for the template argument of the item type, even though the handles have not been defined at this point. However, it is the difference between declaration and definition that allows us to use the declared type of the Container for the item type instantiation.
    With respect to this instantiation the idea of this code is simuliar to Curiously Recurring Template Pattern:
    Code:
    template <class T>
    class Base
    {
    	.......
    };
    class Curious:public Base<Curious>
    {
    	........
    };

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, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM