Thread: Am I doing something wrong or does my compiler not understand this?

  1. #1
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838

    Am I doing something wrong or does my compiler not understand this?

    trying to follow this:
    http://www.ddj.com/cpp/184403813

    all seemed to be going fairly well, but i am afraid my compiler (BCB6) might not be so hot at template deduction.

    Code:
    template<typename H,typename T>struct typelist
    {
        typedef H head;
        typedef T tail;
    };
    
    struct end{};
    template<typename T1,typename T2,typename T3,typename T4> struct types;//i had to add this to make dr dobb's example compile at all
    
    template<typename T> struct types<T,end,end,end>
    {
        typedef typelist<T,end> type;
    };
    
    template<typename T1,typename T2>struct types<T1,T2,end,end>
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,end
            >
        >
        type;
    };
    
    template<typename T1,typename T2,typename T3>struct types<T1,T2,T3,end>
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,typelist
                <
                    T3,end
                >
            >
        >
        type;
    };
    
    template<typename T1,typename T2,typename T3,typename T4>struct types<T1,T2,T3,T4>
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,typelist
                <
                    T3,
                    typelist
                    <
                        T4,end
                    >
                >
            >
        >
        type;
    };
    
    typedef types<int,char>::type intchar; // compiles until i add this line; error = 'too few arguments passed to types
    
    typedef types<int,char,end,end>::type intchar; // this compiles but does not exactly work like a typelist :(
    any ideas what gives? i don't usually second-guess my compiler, but modifying examples to make them compile makes me suspicious.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    You've somewhat garbled the order and attempted to specialize something that isn't.

    Soma

    Code:
    template<typename H,typename T>struct typelist
    {
        typedef H head;
        typedef T tail;
    };
    
    struct end{};
    
    template<typename T1,typename T2 = end,typename T3 = end,typename T4 = end>struct types
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,typelist
                <
                    T3,
                    typelist
                    <
                        T4,end
                    >
                >
            >
        >
        type;
    };
    
    template<typename T> struct types<T,end,end,end>
    {
        typedef typelist<T,end> type;
    };
    
    template<typename T1,typename T2>struct types<T1,T2,end,end>
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,end
            >
        >
        type;
    };
    
    template<typename T1,typename T2,typename T3>struct types<T1,T2,T3,end>
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,typelist
                <
                    T3,end
                >
            >
        >
        type;
    };
    
    typedef types<int,char>::type intchar; // compiles until i add this line; error = 'too few arguments passed to types
    
    typedef types<int,char,end,end>::type intchar; // this compiles but does not exactly work like a typelist :(

  3. #3
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    thanks. i saw something like that in another page on the subject, but the good doctor didn't include that in his example.

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    can you help me understand (or point me to a better reference for) how to recursively instantiate an object based on a typelist? his visitor pattern doesn't work as provided either...

    failing that, can you point me to a reference to better understand the finer rules of templates in general? there seems to be so much more to them than is in the documentation.

    Code:
    template<typename T>class metaObject;
    template<typename H,typename T>class metaObject<types<H,T> >:
        public metaObject<T>
    {
        public:
            metaObject() :
                metaObject<T>()
            {
            }
        H *h;
    };
    
    void test()
    {
        metaObject< types<int,double> >o;
        o.metaObject<int>::h = 2;
    }

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    How about getting a new compiler? Visual Studio Express is free, and so is Code::Blocks with MinGW.
    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

  6. #6
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    i don't think the time required to port the code will be an allowable amount of downtime. i am the only developer at my company :\

    anyway, as to the question at hand, am i doing something wrong here? it really looks like it should recurse on the tail properly as i've written it, but i am just starting to try to more fully utilize templates. as i alluded to before, sometimes the more subtle intricacies escape me.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Where in the article did you even find this example? The link you posted does not contain anything resembling it.
    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
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    template <class tlist> class AdHocVisitor;
    
    template <class H, class T>
    class AdHocVisitor< typelist<H, T> > 
        : public AdHocVisitor<T>
    {
    public:
        using AdHocVisitor<T>::Visit;
        virtual void Visit(H*) = 0;
        template <class SomeClass>
        void StartVisit(SomeClass* p)
        {
            if (H* pFound = dynamic_cast<H*>(p))
            {
                Visit(pFound);
            }
            else
            {
                AdHocVisitor<T>::StartVisit(p);
            }
        }
    };
    
    template <class H>
    class AdHocVisitor< typelist<H, null_typelist> > 
    {
    public:
        virtual void Visit(H*) = 0;
        template <class SomeClass>
        void StartVisit(SomeClass* p)
        {
            if (H* pFound = dynamic_cast<H*>(p))
            {
                Visit(pFound);
            }
            else
            {
                throw "Unknown type passed";
            }
        }
    };
    i ran into exactly the same errors when i partially specialized for when tail is null_typelist (or end, as i called it above). - it said that metaObject<type> had to be a previously defined class or struct...

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    metaObject?

    Can you post a complete code snippet that exhibits the error? That I can test on a different compiler.
    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
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    //---------------------------------------------------------------------------
    
    #ifndef metaObjectH
    #define metaObjectH
    template<typename H,typename T>struct typelist
    {
        typedef H head;
        typedef T tail;
    };
    
    struct end{};
    
    template<typename T1,typename T2 = end,typename T3 = end,typename T4 = end>struct types
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,typelist
                <
                    T3,
                    typelist
                    <
                        T4,end
                    >
                >
            >
        >
        type;
    };
    
    template<typename T> struct types<T,end,end,end>
    {
        typedef typelist<T,end> type;
    };
    
    template<typename T1,typename T2>struct types<T1,T2,end,end>
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,end
            >
        >
        type;
    };
    
    template<typename T1,typename T2,typename T3>struct types<T1,T2,T3,end>
    {
        typedef
        typelist
        <
            T1,typelist
            <
                T2,typelist
                <
                    T3,end
                >
            >
        >
        type;
    };
    
    
    
    
    template<typename T>class metaObject;
    template<typename H,typename T>class metaObject< types<H,T> >:
        public metaObject<T>
    {
        public:
        H *value;
    };
    
    template<typename H>class metaObject<types<H,end> >
    {
        public:
        H *value;
    };
    
    
    void test()
    {
        metaObject< types<double,int> >o;
        *o.metaObject<int>::value=2;
    }
    //---------------------------------------------------------------------------
    #endif
    Build
    [C++ Error] metaObject.h(71): E2029 'metaObject<int>' must be a previously defined class or struct
    [C++ Error] metaObject.h(87): E2450 Undefined structure 'metaObject<double,int>'
    [C++ Error] metaObject.h(87): E2449 Size of 'o' is unknown or zero
    [C++ Error] metaObject.h(87): E2450 Undefined structure 'metaObject<double,int>'
    [C++ Error] metaObject.h(88): E2312 'metaObject<int>' is not an unambiguous base class of 'metaObject<double,int>'



    i tried to DL VS Express, but the installer hung on me and now i have to RB to try it again

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    template<typename T>class metaObject;
    This is a forward declaration, not a definition. As thus, the class does not exist; you are merely telling the compiler that it does exist, which in reality, it does not.

    Code:
    template<typename T>class metaObject { };
    This should fix the error, I think, or at least one of them, but begin with that.
    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.

  12. #12
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    not bad! i had to make this slight modification:

    Code:
    template<typename H>class metaObject {public:H* value; };

    to do what i wanted, but yes, this solved the errors.

    i can now do the following:

    Code:
    void test()
    {
        metaObject< types<double,int> >o;
        o.metaObject<int>::value = new int(2);
        o.metaObject<types<double,int> >::value = new double(1.234);
    }
    now if i can just figure out a more elegant way of specifying where in the tail hierarchy a particular value is, i'll really have something potentially useful.
    Last edited by m37h0d; 01-13-2009 at 12:59 PM.

  13. #13
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Quote Originally Posted by m37h0d View Post

    now if i can just figure out a more elegant way of specifying where in the tail hierarchy a particular value is, i'll really have something potentially useful.
    to that end, i think the implementation suggested here is a good fit. if each tier in the hierarchy can keep track of it's parent typelist, then one ought to be able to construct a template getter and setter functions where you specify the ordinal within the recurion/inheritance tree of the value you want...

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Why do you instantiate metaObject<int>?

    Elysia is trying to cure the symptoms, but I really think there's a conceptual problem here.
    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

  15. #15
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    there are undoubtedly many conceptual problems. the foremost being the classical problem of "an answer in search of a problem"

    i am really trying to explore this area of programming and see what its capabilities are and how it might be useful. i have ideas about how things like typelists and recursive template instantiation might be useful, but i don't have enough familiarity with them to really know. i'm trying to find out!


    and to answer your question:

    because for the purposes of this exploratory example, i wanted o to contain a place to store an int?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. compiler differences
    By white in forum C Programming
    Replies: 1
    Last Post: 02-18-2005, 02:58 PM
  3. Comile problem using latest Dev C++ Compiler
    By shiny_dico_ball in forum C++ Programming
    Replies: 6
    Last Post: 06-06-2003, 05:32 PM
  4. C Compiler
    By SAMSEIED in forum C Programming
    Replies: 5
    Last Post: 06-06-2002, 05:44 PM
  5. What's wrong with Micorsoft Visual C++?
    By knave in forum C++ Programming
    Replies: 9
    Last Post: 10-04-2001, 08:02 AM

Tags for this Thread