problem calling method from parent of an object if object relayson template arguments of current tem

Discussion in 'C++' started by =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=, Mar 2, 2006.

  1. Test case:

    #include <boost/shared_ptr.hpp>

    using namespace boost;

    template <typename typ1> class cData {
    public:
    template <typename T> T As() { } // As
    };


    template <typename D, typename T>
    class cTaggedData : public cData<D> {
    public:
    };

    template <typename D, typename T>
    class cAttr : public cTaggedData<D,T> {
    public:
    };

    template <typename D, typename T>
    class cNode : public cTaggedData<D,T> {
    public:

    shared_ptr< cAttr<D,T> > RetAttr() { }
    template <typename X> X MyAs(T t, X& x) {
    #line 100
    shared_ptr< cAttr<int,int> > attr1; attr1->As<int>(); // works
    #line 200
    shared_ptr< cAttr<D,T> > attr2; attr2->As<int>(); // FAILS
    #line 300
    shared_ptr< cAttr<int,int> > attr3; attr3->As<X>(); // works
    #line 400
    shared_ptr< cAttr<D,T> > attr4; attr4->As<X>(); // my goal, FAILS
    } // As

    };
    /*
    gives:

    x.cpp: In member function 'X cNode<D, T>::MyAs(T, X&)':
    x.cpp:200: error: expected primary-expression before 'int'
    x.cpp:200: error: expected `;' before 'int'
    x.cpp:400: error: expected primary-expression before '>' token
    x.cpp:400: error: expected primary-expression before ')' token
    */

    int main() { }

    How to fix thoes two errors? Especially the 400 one?
    =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=, Mar 2, 2006
    #1
    1. Advertising

  2. * Rafał Maj Raf256:
    > Test case:
    >
    > #include <boost/shared_ptr.hpp>
    >
    > using namespace boost;
    >
    > template <typename typ1> class cData {
    > public:
    > template <typename T> T As() { } // As
    > };
    >
    >
    > template <typename D, typename T>
    > class cTaggedData : public cData<D> {
    > public:
    > };
    >
    > template <typename D, typename T>
    > class cAttr : public cTaggedData<D,T> {
    > public:
    > };
    >
    > template <typename D, typename T>
    > class cNode : public cTaggedData<D,T> {
    > public:
    >
    > shared_ptr< cAttr<D,T> > RetAttr() { }
    > template <typename X> X MyAs(T t, X& x) {
    > #line 100
    > shared_ptr< cAttr<int,int> > attr1; attr1->As<int>(); // works
    > #line 200
    > shared_ptr< cAttr<D,T> > attr2; attr2->As<int>(); // FAILS
    > #line 300
    > shared_ptr< cAttr<int,int> > attr3; attr3->As<X>(); // works
    > #line 400
    > shared_ptr< cAttr<D,T> > attr4; attr4->As<X>(); // my goal, FAILS
    > } // As
    >
    > };
    > /*
    > gives:
    >
    > x.cpp: In member function 'X cNode<D, T>::MyAs(T, X&)':
    > x.cpp:200: error: expected primary-expression before 'int'
    > x.cpp:200: error: expected `;' before 'int'
    > x.cpp:400: error: expected primary-expression before '>' token
    > x.cpp:400: error: expected primary-expression before ')' token
    > */


    Using #line is a really good idea, I'll steal that for my own examples!

    Anyway,

    #line 100
    shared_ptr< cAttr<int,int> > attr1; attr1->As<int>(); // works
    #line 200
    shared_ptr< cAttr<D,T> > attr2; attr2->template As<int>(); // works
    #line 300
    shared_ptr< cAttr<int,int> > attr3; attr3->As<X>(); // works
    #line 400
    shared_ptr< cAttr<D,T> > attr4; attr4->template As<X>(); // works

    The problem is that you're using the name As which depends on the
    template paramters D and T, and the compiler knows nothing about what As
    could be (especially, that it would be a template) for any specific D and T.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 2, 2006
    #2
    1. Advertising

  3. Alf P. Steinbach wrote:

    > Using #line is a really good idea, I'll steal that for my own examples!


    Nice :)

    > #line 100
    > shared_ptr< cAttr<int,int> > attr1; attr1->As<int>(); // works
    > #line 200
    > shared_ptr< cAttr<D,T> > attr2; attr2->template As<int>(); // works
    > #line 300
    > shared_ptr< cAttr<int,int> > attr3; attr3->As<X>(); // works
    > #line 400
    > shared_ptr< cAttr<D,T> > attr4; attr4->template As<X>(); // works


    > The problem is that you're using the name As which depends on the
    > template paramters D and T, and the compiler knows nothing about what As
    > could be (especially, that it would be a template) for any specific D
    > and T.


    #line 200
    shared_ptr< cAttr<D,T> > attr2; attr2->template As<int>();

    #line 400
    shared_ptr< cAttr<D,T> > attr4; attr4->template As<X>();

    is the finall solution then :)

    I was quite close, tried to add, in the main class { } cNode scope
    using cAttr<D,T>::As<>;
    g++ warned me:
    note: use 'cAttr<D, T>::template As' to indicate that it is a template
    only I put it in wrong place

    btw, it seem to compile fine on newer MSVC even without the template

    --
    Wymiana starych układów... na nowe układy - prawie jak walka z korupcja.
    Walka z wychowaniem seksualnym i erotykÄ… - prawie jak walka z patologiÄ….
    PiS - prawie jak prawo i sprawiedliwość... Prawie. Prawie robi różnicę.
    Myśl. Głosuj rozsądnie. Nie na tanie hasła. // Rafał Maj Raf256
    =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=, Mar 2, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. girays
    Replies:
    1
    Views:
    665
  2. Replies:
    1
    Views:
    2,100
    Gianni Mariani
    Jun 8, 2007
  3. Navaneeth
    Replies:
    4
    Views:
    550
    Kenny McCormack
    Nov 20, 2010
  4. Matthew Heidemann
    Replies:
    14
    Views:
    244
    eT Ma
    Mar 22, 2011
  5. Replies:
    3
    Views:
    176
Loading...

Share This Page