-pedantic

-pedantic

Post by Christopher Thompso » Fri, 19 May 2000 04:00:00



#include <vector>

template <class T> void foo(vector<T> bar) {
vector<T>::iterator i;

Quote:}

Compiled with g++ -pedantic x.cc, this fails with:

x.cc: In function `void foo(class vector<T,__default_alloc_template<true,0> >)':
x.cc:4: parse error before `;'

Any idea why?

--
Christopher Thompson

 
 
 

-pedantic

Post by TiTi » Fri, 19 May 2000 04:00:00


Quote:> #include <vector>

> template <class T> void foo(vector<T> bar) {
> vector<T>::iterator i;
> }

Try:

   #include <vector>
    using std::vector;

    template <class T>
    void foo(vector<T>& bar)
    {
        vector<T>::iterator i;
    }

Quote:

> Compiled with g++ -pedantic x.cc, this fails with:

> x.cc: In function `void foo(class vector<T,__default_alloc_template<true,0> >)':
> x.cc:4: parse error before `;'

> Any idea why?

TT

 
 
 

-pedantic

Post by Thomas Perschk » Fri, 19 May 2000 04:00:00


Try

    template <class T>
    void foo(vector<T>& bar)
    {
       typename  vector<T>::iterator i;
    }

 Thomas Perschke


> #include <vector>

> template <class T> void foo(vector<T> bar) {
> vector<T>::iterator i;
> }

> Compiled with g++ -pedantic x.cc, this fails with:

> x.cc: In function `void foo(class vector<T,__default_alloc_template<true,0> >)':
> x.cc:4: parse error before `;'

> Any idea why?

> --
> Christopher Thompson


 
 
 

-pedantic

Post by Christopher Thompso » Fri, 19 May 2000 04:00:00


: Try:

:    #include <vector>
:     using std::vector;

:     template <class T>
:     void foo(vector<T>& bar)
:     {
:         vector<T>::iterator i;
:     }

Doesn't help.  It turns out that doing:
  typename  vector<T>::iterator i;

makes it work with -pedantic.  I'm not sure why.

--
Christopher Thompson

 
 
 

-pedantic

Post by Christopher Thompso » Fri, 19 May 2000 04:00:00


Thanks.  This works.  Any idea why?

: Try

:     template <class T>
:     void foo(vector<T>& bar)
:     {
:        typename  vector<T>::iterator i;
:     }

--
Christopher Thompson

 
 
 

-pedantic

Post by Dietmar Kueh » Fri, 19 May 2000 04:00:00


Hi,


Quote:> Thanks.  This works.  Any idea why?

In you function 'vector<T>::iterator' is a name depending on a template
argument, in this case the type 'T'. For dependend names you have to
use 'typename' to indicate that the name refers to a type. Otherwise
the name is not considered to be a type. The reasons why this is need
are somewhat obscure, though... (when using the separation model the
compiler needs to tell what is going on without have access to the
actual instantiation; in this case, there are situations where the
compiler cannot deduce whether a name would be a type or not).
--

homepage: <http://www.informatik.uni-konstanz.de/~kuehl>

Sent via Deja.com http://www.deja.com/
Before you buy.

 
 
 

-pedantic

Post by Thomas Perschk » Fri, 19 May 2000 04:00:00



Quote:> Thanks.  This works.  Any idea why?

If I would really understand it, I would explain you why

template <class T >
class vector {
public:

  typedef T* iterator;
  vector();
 ~vector();

Quote:};

template<class T>
void foo(vector<T>& bar)
     {
      vector<T>::iterator i;
     }

compiles fine with -pedantic :-)
Maybe someone else can enlight us?

Thomas Perschke


> : Try

> :     template <class T>
> :     void foo(vector<T>& bar)
> :     {
> :        typename  vector<T>::iterator i;
> :     }

> --
> Christopher Thompson


 
 
 

-pedantic

Post by Christopher Thompso » Fri, 19 May 2000 04:00:00


: If I would really understand it, I would explain you why

Someone emailed me an answer.  Here it is:

In you function 'vector<T>::iterator' is a name depending on a template
argument, in this case the type 'T'. For dependend names you have to
use 'typename' to indicate that the name refers to a type. Otherwise
the name is not considered to be a type. The reasons why this is need
are somewhat obscure, though... (when using the separation model the
compiler needs to tell what is going on without have access to the
actual instantiation; in this case, there are situations where the
compiler cannot deduce whether a name would be a type or not).

(I snipped his name because I didn't know if it was okay to include
it here :)

--
Christopher Thompson

 
 
 

-pedantic

Post by Thomas Perschk » Fri, 19 May 2000 04:00:00


This was my reasoning when I told you the answer.
But I don't understand why the following code is valid  with g++ -pedantic

template <class T >
class vector {
public:

  typedef T* iterator;
  vector();
 ~vector();

Quote:};

template<class T>
void foo(vector<T>& bar)
     {
      vector<T>::iterator i;
     }

when I omit the 'typename'. I expected the same behaviour as in
your original code. Still waiting for enlightment.

Thomas Perschke



> : If I would really understand it, I would explain you why

> Someone emailed me an answer.  Here it is:

> In you function 'vector<T>::iterator' is a name depending on a template
> argument, in this case the type 'T'. For dependend names you have to
> use 'typename' to indicate that the name refers to a type. Otherwise
> the name is not considered to be a type. The reasons why this is need
> are somewhat obscure, though... (when using the separation model the
> compiler needs to tell what is going on without have access to the
> actual instantiation; in this case, there are situations where the
> compiler cannot deduce whether a name would be a type or not).

> (I snipped his name because I didn't know if it was okay to include
> it here :)

> --
> Christopher Thompson


 
 
 

1. STL map and gcc -pedantic

   The following code

#include <map>
using std::map;

template < class T >
T lookup ( const map <int, T> amap, int i )
{
  map< int, T >::const_iterator it = amap.find ( i );
  return it->second;

int main ()
{
  map<int,int> amap;
  return lookup ( amap, 0 );

leads to error messages...

c++ -pedantic -c main.cxx
main.cxx: In function `T lookup(map<int,T,less<int>,allocator<_Tp1> >, int)':
main.cxx:7: parse error before `='
main.cxx: In function `int lookup<int>(map<int,int,less<int>,allocator<int> >, int)':
main.cxx:14:   instantiated from here
main.cxx:8: `it' undeclared (first use this function)
main.cxx:8: (Each undeclared identifier is reported only once
main.cxx:8: for each function it appears in.)
main.cxx:8: warning: control reaches end of non-void function `lookup<int>(map<int,int,less<int>,allocator<int> >, int)'

If I take the `-pedantic' flag away, it compiles.   Anybody understand that?

2. PV Hard Case - Availability??

3. parse error with STL classes and -pedantic option

4. Hi All!

5. Q: Strange "gcc -pedantic" problem...

6. Upgrade NT 4 to 2000 question

7. Pedantic point about SWITCH documentation

8. Installation Question

9. -pedantic, -ansi --> not really recommended in GNU CC/RHIDE Help?

10. Problem with -pedantic & basic_string inlines

11. easy-to-use TeX-like tool (Was: Re: gcc -pedantic)

12. Pedantic protection

13. Running the SPEC benchmarks (pedantic questions)?