Thread: template specialization problem

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    template specialization problem

    Hello

    I would like to do:

    Code:
    std::list<some_templated_class> _list;
    _list.push_back(some_templated_class<some_object>(arguments));
    but my compiler wont allow me to compile this. I get error:
    unspecialized class template can't be used as a template argument for template parameter '_Ty', expected a real type

    Is there any other way to be able to do/override this?

    Thanks for help

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    std::list<some_templated_class<some_object> > _list;
    You can't have the list hold templated objects of different types, since each is actually a different class, so you just need to specify the type for the some_templated_class.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by Daved View Post
    Code:
    std::list<some_templated_class<some_object> > _list;
    You can't have the list hold templated objects of different types, since each is actually a different class, so you just need to specify the type for the some_templated_class.
    I want to store handler within some_templated_class.
    For instance:

    some_templated_class<boost:bind(arguments)> _list;

    Each would be build with boost::bind but with different arguments.
    Is this possible to do?

    Maybe I could use boost::any or boost::tuple?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You'll need to store the pointers on the list instead, if the classes are not of identical type. Perhaps a boost ptr_container.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by iMalc View Post
    You'll need to store the pointers on the list instead, if the classes are not of identical type. Perhaps a boost ptr_container.
    Is this the only solution?
    Does boost ptr_map exist?

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    630
    However, I get the same error with
    Code:
    boost::ptr_vector<some_templated_class> _list;
    Is there something I'm doing wrong?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I don't see how storing pointers will help, you still have to provide the type of the pointer.

    I don't actually know boost::bind well enough to figure out a good solution. boost::any and boost::variant can be used when you will have different types, so that is an option, but I don't know if it is a good option.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks for help Daved. Maybe someone else knows?

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by l2u View Post
    Thanks for help Daved. Maybe someone else knows?
    Store pointers to a base class and dispatch functionality through virtuals. In other words, derive the some_templated_class from a common NON-template base:

    Code:
    class my_base
    {
    public:
        virtual ~my_base(); // always need this
    
        virtual void do_this();
        virtual void do_that();
        // etc...
    };
    
    template <typename T>
    class some_templated_class : public my_base
    {
        // ...
    };
    Now, you store my_base pointers in the list, not instances:

    Code:
    std::list<my_base *> _list;
    _list.push_back(new some_templated_class<some_object>(arguments));
    You'll have to take care of cleaning up the dynamically allocated instances when the _list goes away. You can do that by using some kind of smart pointer.

  10. #10
    Registered User
    Join Date
    May 2006
    Posts
    630
    Cant I use boost ptr_container for this?

  11. #11
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Right. Then you wouldn't have to worry about the last thing brewbuck said.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Thanks brewbuck. That was what I was getting at.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    In this case, however, boost::function (or tr1::function) may well be the type erasure root you seek, since what you want to store is bind expressions.
    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
    630
    Quote Originally Posted by CornedBee View Post
    In this case, however, boost::function (or tr1::function) may well be the type erasure root you seek, since what you want to store is bind expressions.
    I took a look at boost function..
    Is it possible to call boost::function with unknown arguments?

    For instance:

    Code:
    boost::function<void(void)> func;
    func = boost::bind(&Test::func,&test, someargument, argument2);

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No, a compatible signature of the function must be given.
    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. Singleton template problem
    By cloudy in forum C++ Programming
    Replies: 14
    Last Post: 01-11-2009, 05:40 AM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Template Partial Specialization
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2007, 11:05 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Template function specialization
    By thomas.behr in forum C++ Programming
    Replies: 4
    Last Post: 06-03-2003, 11:50 AM