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

G

Guest

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?
 
A

Alf P. Steinbach

* 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.
 
G

Guest

Alf said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top