Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typedefs and templates

Reply
Thread Tools

typedefs and templates

 
 
kf3cr
Guest
Posts: n/a
 
      10-05-2009

I am including the code that does not compile below. I am curious as to
why this does not compile, and I would appreciate it if anybody knew
where in the c++ standard I could info to better understand this issue.

For the sake of this post I am going to assume that gcc 4.2.1 is better
with the c++ standard than my copy of visual studio 2009 version
9.0.30729.1 SP since gcc gives the error and visual studio compiles it.

gcc Error text:
main.cpp: In member function 'void B<G>::f()':
main.cpp:22: error: expected ';' before 'itr2'


#include <vector>

template <typename T>
class A
{
public:
typedef std::vector<int> stuffs;
typedef stuffs::const_iterator ConstIterator;
};


template <typename G>
class B
{

public:
void f(void)
{
A<bool>::ConstIterator itr1; // this is ok

A<G>::ConstIterator itr2; // Problem here
}
};

int main(void) {return 0;}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-05-2009
kf3cr wrote:
> I am including the code that does not compile below. I am curious as to
> why this does not compile, and I would appreciate it if anybody knew
> where in the c++ standard I could info to better understand this issue.
>
> For the sake of this post I am going to assume that gcc 4.2.1 is better
> with the c++ standard than my copy of visual studio 2009 version
> 9.0.30729.1 SP since gcc gives the error and visual studio compiles it.
>
> gcc Error text:
> main.cpp: In member function 'void B<G>::f()':
> main.cpp:22: error: expected ';' before 'itr2'
>
>
> #include <vector>
>
> template <typename T>
> class A
> {
> public:
> typedef std::vector<int> stuffs;
> typedef stuffs::const_iterator ConstIterator;
> };
>
>
> template <typename G>
> class B
> {
>
> public:
> void f(void)


C-ism: omit the 'void' when defining an empty parameter list. Don't put
anything where nothing is expected. Just write

void f()

While it's not an error, it clutters the code with something that is
totally unnecessary.

> {
> A<bool>::ConstIterator itr1; // this is ok
>
> A<G>::ConstIterator itr2; // Problem here


'ConstIterator' is a dependent name. Turn up the warning level on VC++
or disable the language extensions, and you may see the problem.

The solution is to use 'typename' before the type name:

typename A<G>::ConstIterator itr2;

> }
> };
>
> int main(void) {return 0;}



V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
kf3cr
Guest
Posts: n/a
 
      10-05-2009
Victor Bazarov wrote:
> kf3cr wrote:
>> I am including the code that does not compile below. I am curious as to
>> why this does not compile, and I would appreciate it if anybody knew
>> where in the c++ standard I could info to better understand this issue.
>>
>> For the sake of this post I am going to assume that gcc 4.2.1 is better
>> with the c++ standard than my copy of visual studio 2009 version
>> 9.0.30729.1 SP since gcc gives the error and visual studio compiles it.
>>
>> gcc Error text:
>> main.cpp: In member function 'void B<G>::f()':
>> main.cpp:22: error: expected ';' before 'itr2'
>>
>>
>> #include <vector>
>>
>> template <typename T>
>> class A
>> {
>> public:
>> typedef std::vector<int> stuffs;
>> typedef stuffs::const_iterator ConstIterator;
>> };
>>
>>
>> template <typename G>
>> class B
>> {
>>
>> public:
>> void f(void)

>
> C-ism: omit the 'void' when defining an empty parameter list. Don't put
> anything where nothing is expected. Just write
>
> void f()
>
> While it's not an error, it clutters the code with something that is
> totally unnecessary.


oops, I always forget that, and somebody always points it out. I don't
think I will ever break that.

>
>> {
>> A<bool>::ConstIterator itr1; // this is ok
>>
>> A<G>::ConstIterator itr2; // Problem here

>
> 'ConstIterator' is a dependent name. Turn up the warning level on VC++
> or disable the language extensions, and you may see the problem.
>
> The solution is to use 'typename' before the type name:
>
> typename A<G>::ConstIterator itr2;


HA! When in doubt, add typename. Thanks so much for helping. I can't
believe I missed this. Every other line seems to need an extra typename
or two, and I usually catch these errors. Thanks again.

>
>> }
>> };
>>
>> int main(void) {return 0;}

>
>
> V

 
Reply With Quote
 
Michael Doubez
Guest
Posts: n/a
 
      10-06-2009
On 5 oct, 22:11, kf3cr <(E-Mail Removed)> wrote:
> Victor Bazarov wrote:
> > kf3cr wrote:
> >> I am including the code that does not compile below. *I am curious as to
> >> why this does not compile, and I would appreciate it if anybody knew
> >> where in the c++ standard I could info to better understand this issue..

>
> >> For the sake of this post I am going to assume that gcc 4.2.1 is better
> >> with the c++ standard than my copy of visual studio 2009 version
> >> 9.0.30729.1 SP since gcc gives the error and visual studio compiles it..

>
> >> gcc Error text:
> >> main.cpp: In member function 'void B<G>::f()':
> >> main.cpp:22: error: expected ';' before 'itr2'

>
> >> #include <vector>

>
> >> template <typename T>
> >> class A
> >> {
> >> * * public:
> >> * * * * typedef std::vector<int> stuffs;
> >> * * * * typedef stuffs::const_iterator ConstIterator;
> >> };

>
> >> template <typename G>
> >> class B
> >> {

>
> >> * * public:
> >> * * * * void f(void)

>
> > C-ism: omit the 'void' when defining an empty parameter list. *Don't put
> > anything where nothing is expected. *Just write

>
> > * * * * * * * * void f()

>
> > While it's not an error, it clutters the code with something that is
> > totally unnecessary.

>
> oops, I always forget that, and somebody always points it out. *I don't
> think I will ever break that.
>
>
>
> >> * * * * {
> >> * * * * * * A<bool>::ConstIterator itr1; // this is ok

>
> >> * * * * * * A<G>::ConstIterator itr2; // Problem here

>
> > 'ConstIterator' is a dependent name. *Turn up the warning level on VC++
> > or disable the language extensions, and you may see the problem.

>
> > The solution is to use 'typename' before the type name:

>
> > * * * * * * * * * * typename A<G>::ConstIterator itr2;

>
> HA! When in doubt, add typename. *Thanks so much for helping. *I can't
> believe I missed this. *Every other line seems to need an extra typename
> or two, and I usually catch these errors. *Thanks again.


Well. With the current standard, putting a typename where it is not
needed is an error.

IIRC the next standard will release that restriction and you will be
able to happily litter your code with 'typename' all over the
source

--
Michael
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about Pointer to Member Typedefs inside templates wbasheer@gmail.com C++ 1 01-25-2008 07:18 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Forward declarations, templates, and typedefs Noah Roberts C++ 7 05-02-2006 11:02 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
Templates and Typedefs dwrayment C++ 6 08-14-2003 05:52 AM



Advertisments