implicit typename problem

X

xuatla

Hi,

When I compile the following test code I got a warning about implicit
typename. This happens in the member functions. Do you know the detail
reason and solution? Thanks.

- X

----------

test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
test.cpp:29: warning: implicit typename is deprecated, please see the
documentation for details
test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
test.cpp:29: warning: implicit typename is deprecated, please see the
documentation for details

-----------
// -- source code: test.cpp

#include <iostream>
using namespace std;

template <typename T>
class cBase
{
public:
typedef unsigned int size_type;
typedef T val_type;
typedef T* ptr_type;

public:
cBase()
{
// ...
} ;
} ;

template <class T>
class cDerived : public cBase<T>
{
public:
cDerived()
{
// ...
} ;
//line 28 here
ptr_type getMemory(ptr_type& p) // compiling warning: see above
{
return p;
} ;
} ;

int main()
{
cDerived<double>::size_type i = 0; // OK
cDerived<double>::val_type d = 0.1; // OK
cDerived<double>::ptr_type p = new double[10]; // OK

return 1;
}
 
V

Victor Bazarov

xuatla said:
When I compile the following test code I got a warning about implicit
typename. This happens in the member functions. Do you know the detail
reason and solution? Thanks.

This is covered in the FAQ. Look for "dependent names".

V
 
T

Thomas Tutone

xuatla said:
When I compile the following test code I got a warning about implicit
typename. This happens in the member functions. Do you know the detail
reason and solution? Thanks.

- X

----------

test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
test.cpp:29: warning: implicit typename is deprecated, please see the
documentation for details
test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
test.cpp:29: warning: implicit typename is deprecated, please see the
documentation for details

-----------
// -- source code: test.cpp

#include <iostream>
using namespace std;

template <typename T>
class cBase
{
public:
typedef unsigned int size_type;
typedef T val_type;
typedef T* ptr_type;

public:
cBase()
{
// ...
} ;
} ;

template <class T>
class cDerived : public cBase<T>
{
public:
cDerived()
{
// ...
} ;
//line 28 here
ptr_type getMemory(ptr_type& p) // compiling warning: see above
{
return p;
} ;
} ;

int main()
{
cDerived<double>::size_type i = 0; // OK
cDerived<double>::val_type d = 0.1; // OK
cDerived<double>::ptr_type p = new double[10]; // OK

return 1;
}

The FAQ answers this:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

I expect you'll find the rest of the FAQ quite helpful as well.

Best regards,

Tom
 
X

xuatla

Victor said:
xuatla said:
When I compile the following test code I got a warning about implicit
typename. This happens in the member functions. Do you know the detail
reason and solution? Thanks.


This is covered in the FAQ. Look for "dependent names".



V
Thank you.

I read that part ([35.18], [35.19]) but still can't figure out the
solution for this problem. The one discussed in FAQ is about dependent
name of member functions, not on the typename. (for member function we
may change f() to this->f(), but for typename I have no idea and want to
get help).

I tried to use cBase<T>::ptr_type, but this is still warned as an
implicit typename.

One way I can solve it is to redefine ptr_type in the derived class. But
this looks not so efficient to me.

-X
 
X

xuatla

Thomas said:
xuatla wrote:

When I compile the following test code I got a warning about implicit
typename. This happens in the member functions. Do you know the detail
reason and solution? Thanks.

- X

----------

test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
test.cpp:29: warning: implicit typename is deprecated, please see the
documentation for details
test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
test.cpp:29: warning: implicit typename is deprecated, please see the
documentation for details

-----------
// -- source code: test.cpp

#include <iostream>
using namespace std;

template <typename T>
class cBase
{
public:
typedef unsigned int size_type;
typedef T val_type;
typedef T* ptr_type;

public:
cBase()
{
// ...
} ;
} ;

template <class T>
class cDerived : public cBase<T>
{
public:
cDerived()
{
// ...
} ;
//line 28 here
ptr_type getMemory(ptr_type& p) // compiling warning: see above
{
return p;
} ;
} ;

int main()
{
cDerived<double>::size_type i = 0; // OK
cDerived<double>::val_type d = 0.1; // OK
cDerived<double>::ptr_type p = new double[10]; // OK

return 1;
}


The FAQ answers this:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

I expect you'll find the rest of the FAQ quite helpful as well.

Best regards,

Tom

Thanks to Tom. Yes they are very helpful. I read through the whole FAQ
serveral times before. It seems that not many problems of STL have been
covered in FAQ so far. The reason I want to use typename is because I
want to write something close to STL.

-X
 
V

Victor Bazarov

xuatla said:
Victor said:
xuatla said:
When I compile the following test code I got a warning about
implicit typename. This happens in the member functions. Do you
know the detail reason and solution? Thanks.


This is covered in the FAQ. Look for "dependent names".



V
Thank you.

I read that part ([35.18], [35.19]) but still can't figure out the
solution for this problem. The one discussed in FAQ is about dependent
name of member functions, not on the typename. (for member function we
may change f() to this->f(), but for typename I have no idea and want
to get help).

I tried to use cBase<T>::ptr_type, but this is still warned as an
implicit typename.

Add 'typename' before it. For some reason I thought that the FAQ had
One way I can solve it is to redefine ptr_type in the derived class.

You don't have to.
But this looks not so efficient to me.

What does efficiency have to do with it?

V
 
X

xuatla

Victor said:
xuatla said:
Victor said:
xuatla wrote:


When I compile the following test code I got a warning about
implicit typename. This happens in the member functions. Do you
know the detail reason and solution? Thanks.


This is covered in the FAQ. Look for "dependent names".



[..]


V

Thank you.

I read that part ([35.18], [35.19]) but still can't figure out the
solution for this problem. The one discussed in FAQ is about dependent
name of member functions, not on the typename. (for member function we
may change f() to this->f(), but for typename I have no idea and want
to get help).

I tried to use cBase<T>::ptr_type, but this is still warned as an
implicit typename.


Add 'typename' before it. For some reason I thought that the FAQ had
it. Lemme see... <looking> ...Hmm. Missing. I'll need to contact
Marshall and see if we can add something.

Thank you. With typename it works fine now.
You don't have to.
I agree. And I tried to add the following typedef in cDerived, then I
don't need to specify the scope again.

public:
typedef typename cBase said:
What does efficiency have to do with it?
I mean the coding efficiency... Sorry, I might be too lazy and greedy
 
T

Thomas Tutone

xuatla said:
Thomas said:
xuatla wrote:

When I compile the following test code I got a warning about implicit
typename. This happens in the member functions. Do you know the detail
reason and solution? Thanks.

- X

----------

test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
test.cpp:29: warning: implicit typename is deprecated, please see the
documentation for details
test.cpp:29: warning: `cDerived<T>::ptr_type' is implicitly a typename
test.cpp:29: warning: implicit typename is deprecated, please see the
documentation for details

-----------
// -- source code: test.cpp

#include <iostream>
using namespace std;

template <typename T>
class cBase
{
public:
typedef unsigned int size_type;
typedef T val_type;
typedef T* ptr_type;

public:
cBase()
{
// ...
} ;
} ;

template <class T>
class cDerived : public cBase<T>
{
public:
cDerived()
{
// ...
} ;
//line 28 here
ptr_type getMemory(ptr_type& p) // compiling warning: see above
{
return p;
} ;
} ;

int main()
{
cDerived<double>::size_type i = 0; // OK
cDerived<double>::val_type d = 0.1; // OK
cDerived<double>::ptr_type p = new double[10]; // OK

return 1;
}


The FAQ answers this:

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.18

I expect you'll find the rest of the FAQ quite helpful as well.

Best regards,

Tom

Thanks to Tom. Yes they are very helpful. I read through the whole FAQ
serveral times before. It seems that not many problems of STL have been
covered in FAQ so far. The reason I want to use typename is because I
want to write something close to STL.


You're right - it is not as clear as it should be.

Change line 29 to:

typename cBase<T>::ptr_type getMemory(typename cBase<T>::ptr_type& p)

Best regards,

Tom
 
H

Howard Gardner

Here's some code that might help clear it up for you. OTOH, it might
just make things worse. Good Luck!

#include <cstddef>
#include <ostream>
using namespace std;

struct
base_plain
{
typedef int type;
};

template< typename x >
struct
base_tpt
{
typedef x type;
};

template< typename x >
struct
derived_must
:
public base_tpt< x >
{
// need scope because base_tpt< x > is a dependent type
// need typename because base_tpt< x >::type is a dependent type
typedef typename derived_must::type another_type;
};


template< typename x >
struct
derived_noneed
:
public base_plain
{
// don't need scope because base_plain is not a dependent type
// don't need typename because base_plain::type is not
// a dependent type
type another_type;

// don't need scope because base_plain is not a dependent type
// need typename because base_tpt< x >::type is a dependent type
typedef typename base_tpt< x >::type yet_another_type;
};

int
main()
{
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top