Thread: Can you make an std::list of a templated class.

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not too familiar with boost::any, since it requires you to know the type you are storing to retrieve it.
    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.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Wait, what exactly are you trying to do? Without knowing that we are all just guessing what is the best tool for the job.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by laserlight View Post
    Wait, what exactly are you trying to do? Without knowing that we are all just guessing what is the best tool for the job.
    I am working on a very very simple scripting system. The templated class was used to represent a variable read in by the scipting system. It holds the name, type, and value. The list is actually stored inside the "Scipt Manager".

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps virtual classes are a good solution for this type of job?
    The list stores base classes while exposing virtual functions which the derived classes override and implement.
    Otherwise I figure you're going to have to circumvent the whole type system by checking for type and casting it appropriately, which never is a good idea.
    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.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chadsxe
    The templated class was used to represent a variable read in by the scipting system. It holds the name, type, and value.
    So, in your original model, you wanted to store objects of different instances of the class template in the same std::list, right? What is the motivation for a class template instead of a "normal" Variable class, say a class hierarchy?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by laserlight View Post
    So, in your original model, you wanted to store objects of different instances of the class template in the same std::list, right? What is the motivation for a class template instead of a "normal" Variable class, say a class hierarchy?
    Well the "Variable class" obviously is going to have to store a few different types of variables. Instead of declaring a type of each variable that I will be using in my simple scripting language in the "Variable class", I figured the templated option would be more efficent. The problem is the ScriptingManager class store variable(s) inside of a list.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmmm. Templates and virtual functions does not always play well with each other...
    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.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by chadsxe
    Instead of declaring a type of each variable that I will be using in my simple scripting language in the "Variable class", I figured the templated option would be more efficent.
    As in you are "borrowing" the built-in types of C++? I am not sure how that works with your scripting language, but a possible solution could be to have a VariableType abstract base class, then have a ConcreteVariableType class template inherit from it, since a variable has-a type:
    Code:
    class VariableType
    {
    public:
        virtual ~VariableType() {}
        // ...
    };
    
    template<typename T>
    class ConcreteVariableType : public VariableType
    {
        // ...
    };
    As such, you would have a std::list<Variable>, but each Variable object would have a (smart) pointer to a VariableType, which could point to a ConcreteVariableType<int>, ConcreteVariableType<double>, etc.

    Then again, if your scripting language is so simple, wouldn't it be quite easy to just list all the possible types that the variables have?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by Elysia View Post
    Hmmm. Templates and virtual functions does not always play well with each other...
    ?

    for instance? i haven't had any problems with it.

  10. #25
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Edit: And by the by, the suggestion offered by LL is probably what you need.

    Templates and virtual functions does not always play well with each other...
    They play together just fine so long as you don't treat template methods polymorphically.

    for instance? i haven't had any problems with it.
    Some people expect the following code to print "b:1\nb:0\nd2:0\nd2:2\n" or some other unusual variation. You will not have problems so long as you don't expect the compiler to do magic things.

    Soma

    Code:
    #include <iostream>
    
    struct b
    {
       template
       <
          unsigned int F
       >
       void t()
       {
          std::cout << "b:" << F << '\n';
       }
       virtual void t1()
       {
          t<0>();
       }
       virtual void t2()
       {
          t<0>();
       }
    };
    
    struct d1: b
    {
       virtual void t1()
       {
          t<1>();
       }
    };
    
    struct d2: b
    {
       template
       <
          unsigned int F
       >
       void t()
       {
          std::cout << "d2:" << F << '\n';
       }
       virtual void t2()
       {
          t<2>();
       }
    };
    
    struct dc: d1, d2
    {
    };
    
    int main()
    {
       dc t;
       t.d1::t1();
       t.d1::t2();
       t.d2::t1();
       t.d2::t2();
       return(0);
    }
    Last edited by phantomotap; 02-12-2009 at 10:48 AM.

  11. #26
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by phantomotap View Post

    Some people expect the following code to print "b:1\nb:0\nd2:0\nd2:2\n" or some other unusual variation. You will not have problems so long as you don't expect the compiler to do magic things.

    Soma
    that's an odd thing to expect.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by m37h0d View Post
    ?

    for instance? i haven't had any problems with it.
    The solution that laserlight suggests is one where such a problem may show up.
    Virtual functions just won't work so well with inheritance in such an example, because it doesn't "offer" all the flexibility you might want or need.

    Quote Originally Posted by laserlight View Post
    As in you are "borrowing" the built-in types of C++? I am not sure how that works with your scripting language, but a possible solution could be to have a VariableType abstract base class, then have a ConcreteVariableType class template inherit from it, since a variable has-a type:
    Code:
    class VariableType
    {
    public:
        virtual ~VariableType() {}
        // ...
    };
    
    template<typename T>
    class ConcreteVariableType : public VariableType
    {
        // ...
    };
    As such, you would have a std::list<Variable>, but each Variable object would have a (smart) pointer to a VariableType, which could point to a ConcreteVariableType<int>, ConcreteVariableType<double>, etc.

    Then again, if your scripting language is so simple, wouldn't it be quite easy to just list all the possible types that the variables have?
    Also consider the following example:
    Code:
    class Base
    { 
    public:
    	virtual void foo(int)  = 0 {}
    };
    
    template<typename T> class Derived: public Base
    {
    public:
    	virtual void foo(int) {}
    	virtual void foo(T) {}
    };
    
    int main() 
    {
    	Derived<double> Test;
    	Base& rTest = Test;
    	rTest.foo(10);
    	rTest.foo(1.0);
    }
    The first rTest.foo call will work as expected.
    But the second one will not! It will simply truncate the double to int and call Derived::foo(int) instead of Derived::foo(double).
    Which is why I said they don't play so well together.

    If you want or need a template class, you might as well need to use the template type in the parameter list of some functions. And to call those functions, the base class needs to contain them, as well, as virtual functions, or you'll have to cast them to derived classes, which is wrong.
    But since the base class isn't templated, it cannot represent those virtual functions in the derived class without overloading them all, and that almost kind of beats the purpose of a template class.
    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.

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, but Elysia, your example is due to the fact that the foo() member function from Base takes an int. We could demonstrate this behaviour without templates:
    Code:
    class Base
    { 
    public:
    	virtual void foo(int)  = 0;
    };
    
    class Derived: public Base
    {
    public:
    	virtual void foo(int) {}
    	virtual void foo(double) {}
    };
    
    int main() 
    {
    	Derived Test;
    	Base& rTest = Test;
    	rTest.foo(10);
    	rTest.foo(1.0);
    }
    EDIT:
    Quote Originally Posted by Elysia
    If you want or need a template class, you might as well need to use the template type in the parameter list of some functions. And to call those functions, the base class needs to contain them, as well, as virtual functions, or you'll have to cast them to derived classes, which is wrong.
    But since the base class isn't templated, it cannot represent those virtual functions in the derived class without overloading them all, and that almost kind of beats the purpose of a template class.
    I can agree with your general sentiment, but it depends on the requirements and the design.
    Last edited by laserlight; 02-12-2009 at 11:28 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    Um, but Elysia, your example is due to the fact that the foo() member function from Base takes an int. We could demonstrate this behaviour without templates:
    Which is my point.
    There's just no way to make virtual functions that take T as a parameter without making the base a template class, as well.
    And by doing that, we get the problem of what type is stored inside the list, which was the whole point of a non-template generic base class.
    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.

  15. #30
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    what is "wrong" with:

    Code:
    template<typename T>void abstractClass::getter(T& t)
    {
        t= (static_cast<concreteClass<T>*>(this))->concreteClass<T>::memberOfTypeT;
    }
    template<typename T>void abstractClass::setter(T& t)
    {
        static_cast<concreteClass<T>*>(this))->concreteClass<T>::memberOfTypeT = t;
    }

    you should get a compile-time error if you mangle the types, no?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Overloading templated class operators
    By Artemis0583 in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2006, 12:30 PM
  3. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 11:33 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM