Topic: std list iterator question (using "typename" outside of template).
Author: Dave Sun <hotdog.sun@gmail.com>
Date: Fri, 23 Oct 2009 10:35:45 CST Raw View
The following is my simple test code. When I compile it (with GCC4.2),
I got the following error: "using typename outside of template".
Thank you for your help. This is really frustrating!
#include <iostream>
#include <list>
using namespace std;
int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
list<int> listTest;
for (int i=1; i<10; ++i)
listTest.push_back(i);
for (typename list<int>::const_iterator it = listTest.begin() ; it!
=listTest.end(); ++it) {
cout << *it <<endl;
}
return 0;
}
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Thomas Maeder <maeder@glue.ch>
Date: Fri, 23 Oct 2009 15:56:45 CST Raw View
Dave Sun <hotdog.sun@gmail.com> writes:
> The following is my simple test code. When I compile it (with
> GCC4.2), I got the following error: "using typename outside of
> template".
>
> Thank you for your help. This is really frustrating!
What do you get if you omit "typename"?
And why is this frustrating?
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: usenet@mkarcher.dialup.fu-berlin.de (Michael Karcher)
Date: Fri, 23 Oct 2009 15:56:55 CST Raw View
Dave Sun <hotdog.sun@gmail.com> wrote:
> The following is my simple test code. When I compile it (with GCC4.2),
> I got the following error: "using typename outside of template".
That's a completely correct error message. Just leave off the "typename".
The keyword typename is only needed where the compiler can not yet
determine whether a symbol names a type or not. In you main function,
you use "std::list<int>::const_iterator". At the point the compiler
encounters this line, it can instantiate std::list<int> and find
out by itself that const_iterator names a type.
On the other hand, in the following example (inside a template)
the compiler can *not* instantiate std::list<T> when parsing
the template, as it doesn't know what T is. If it is X,
std::list<T>::const_iterator is a variable, for most other types
it's a type. As templates must be parseable during definition and
not only on instantiation, the keyword typename now is needed.
/* ignoring that we may not specialize in namespace std */
class X {};
namespace std {
template <> class list<int> { int const_iterator; }
};
template <typename T>
void f(T t)
{
std::list<T> list;
list.push_back(t);
for(typename std::list<T>::const_iterator it = list.begin();
it != list.end(); ++it)
cout << *it;
}
Regards,
Michael Karcher
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Fri, 23 Oct 2009 15:56:38 CST Raw View
Dave Sun wrote:
>
> The following is my simple test code. When I compile it (with GCC4.2),
> I got the following error: "using typename outside of template".
well why do you think typename should be allowed there? The language
does not allow artibrary use of keywords on the basis that they just
tell the compiler something it should already know.
Your example does not seem harmful, but it is not supported by the
grammar of C++ and so the compiler is right to reject it.
>
> Thank you for your help. This is really frustrating!
>
>
> #include <iostream>
> #include <list>
> using namespace std;
>
> int main (int argc, char * const argv[]) {
> // insert code here...
> std::cout << "Hello, World!\n";
> list<int> listTest;
> for (int i=1; i<10; ++i)
> listTest.push_back(i);
>
> for (typename list<int>::const_iterator it = listTest.begin() ; it!
> =listTest.end(); ++it) {
> cout << *it <<endl;
> }
>
> return 0;
> }
>
--
Note that robinton.demon.co.uk addresses are no longer valid.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sat, 24 Oct 2009 15:39:27 CST Raw View
On 23 Okt., 18:35, Dave Sun <hotdog....@gmail.com> wrote:
> The following is my simple test code. When I compile it (with GCC4.2),
> I got the following error: "using typename outside of template".
>
> Thank you for your help. This is really frustrating!
>
> #include <iostream>
> #include <list>
> using namespace std;
>
> int main (int argc, char * const argv[]) {
> // insert code here...
> std::cout << "Hello, World!\n";
> list<int> listTest;
> for (int i=1; i<10; ++i)
> listTest.push_back(i);
>
> for (typename list<int>::const_iterator it = listTest.begin() ; it!
> =listTest.end(); ++it) {
> cout << *it <<endl;
> }
>
> return 0;
>
> }
This is a restriction of C++03. Just remove the prefixing "typename"
and the compiler will be lucky (and you, too). In C++03 typename
as type-resolving prefix is only feasible inside a template
declaration,
e.g. consider you wrote a function template like this:
#include <iostream>
#include <list>
using namespace std;
template<class T>
void test() {
std::cout << "Hello, World!\n";
list<T> listTest;
for (int i=1; i<10; ++i)
listTest.push_back(i);
// Prefixing typename is required here:
for (typename list<T>::const_iterator it = listTest.begin();
it!=listTest.end(); ++it) {
cout << *it <<endl;
}
}
int main() {
test<int>();
}
Inside test() the prefixing typename is required, because the compiler
cannot know in advance whether typename list<T>::const_iterator
would be a type or a member. The disambiguation rule is, that it will
be considered as a member inside a template context, if not prefixed
with typename. This means that removing typename in test would
make the code ill-formed, because in the construction
for (list<T>::const_iterator it = expr; ...; ...) {
the existence of "it" does not support a feasible expression.
HTH & Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Greg Herlihy <greghe@mac.com>
Date: Sat, 24 Oct 2009 15:59:58 CST Raw View
On Oct 23, 5:56 pm, Francis Glassborow
<francis.glassbo...@btinternet.com> wrote:
> Dave Sun wrote:
>
> > The following is my simple test code. When I compile it (with GCC4.2),
> > I got the following error: "using typename outside of template".
>
> well why do you think typename should be allowed there? The language
> does not allow artibrary use of keywords on the basis that they just
> tell the compiler something it should already know.
The C++ keywords: "auto", "register" and "inline" certainly seem to
tell the compiler things it should already know (or at least be able
to figure out on its own).
> Your example does not seem harmful, but it is not supported by the
> grammar of C++ and so the compiler is right to reject it.
The unnecessary use of the "typename" keyword is not harmful at all.
In fact, C++0x will allow the "typename" keyword in the example
program, so a C++0x compiler would be wrong to reject it.
Greg
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sat, 24 Oct 2009 20:17:22 CST Raw View
On 24 Okt., 23:59, Greg Herlihy <gre...@mac.com> wrote:
> On Oct 23, 5:56 pm, Francis Glassborow
>
> <francis.glassbo...@btinternet.com> wrote:
> > Dave Sun wrote:
>
> > > The following is my simple test code. When I compile it (with GCC4.2),
> > > I got the following error: "using typename outside of template".
>
> > well why do you think typename should be allowed there? The language
> > does not allow artibrary use of keywords on the basis that they just
> > tell the compiler something it should already know.
>
> The C++ keywords: "auto", "register" and "inline" certainly seem to
> tell the compiler things it should already know (or at least be able
> to figure out on its own).
>
> > Your example does not seem harmful, but it is not supported by the
> > grammar of C++ and so the compiler is right to reject it.
>
> The unnecessary use of the "typename" keyword is not harmful at all.
> In fact, C++0x will allow the "typename" keyword in the example
> program, so a C++0x compiler would be wrong to reject it.
I believe you are misunderstanding Francis' intention here. He is
exactly referencing the expected rules for a C++03-compatible
compiler here and any compiler that would declare itself as
satisfying this standard has to make the OP's code ill-formed,
whether the programmer likes it or not.
C++0x is still under development and not a reference we can
yet refer to as a standard. Even after it's publication there will
be some parts of the language not everyone will like (no, I
don't have something special in my mind, but I would be very
suspicious if someone would try to convince me of this) - this
is just the normal part of any form of evolution and development.
Greetings from Bremen,
Daniel Kr gler
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Sun, 25 Oct 2009 10:11:17 CST Raw View
Greg Herlihy wrote:
> On Oct 23, 5:56 pm, Francis Glassborow
> <francis.glassbo...@btinternet.com> wrote:
>> Dave Sun wrote:
>>
>>> The following is my simple test code. When I compile it (with GCC4.2),
>>> I got the following error: "using typename outside of template".
>> well why do you think typename should be allowed there? The language
>> does not allow artibrary use of keywords on the basis that they just
>> tell the compiler something it should already know.
>
> The C++ keywords: "auto", "register" and "inline" certainly seem to
> tell the compiler things it should already know (or at least be able
> to figure out on its own).
The original auto did but its use in C++0x will be completely different.
register was one of those keywords introduced to allow programmers to
guide a compiler by expressing a preference. Back in the late 70s and
early 80s it was helpful but no longer performs a useful task.
inline is different because it includes the suppression of multiple
definition errors when the definitions are in different TUs.
>
>> Your example does not seem harmful, but it is not supported by the
>> grammar of C++ and so the compiler is right to reject it.
>
> The unnecessary use of the "typename" keyword is not harmful at all.
> In fact, C++0x will allow the "typename" keyword in the example
> program, so a C++0x compiler would be wrong to reject it.
>
Yes, but I am unclear as to what advantage that provides other than
reducing the amount of thought a programmer has to exercise.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Bo Persson" <bop@gmb.dk>
Date: Sun, 25 Oct 2009 10:56:51 CST Raw View
Daniel Kr gler wrote:
> On 24 Okt., 23:59, Greg Herlihy <gre...@mac.com> wrote:
>> On Oct 23, 5:56 pm, Francis Glassborow
>>
>> <francis.glassbo...@btinternet.com> wrote:
>>> Dave Sun wrote:
>>
>>>> The following is my simple test code. When I compile it (with
>>>> GCC4.2), I got the following error: "using typename outside of
>>>> template".
>>
>>> well why do you think typename should be allowed there? The
>>> language does not allow artibrary use of keywords on the basis
>>> that they just tell the compiler something it should already know.
>>
>> The C++ keywords: "auto", "register" and "inline" certainly seem to
>> tell the compiler things it should already know (or at least be
>> able to figure out on its own).
>>
>>> Your example does not seem harmful, but it is not supported by the
>>> grammar of C++ and so the compiler is right to reject it.
>>
>> The unnecessary use of the "typename" keyword is not harmful at
>> all. In fact, C++0x will allow the "typename" keyword in the
>> example program, so a C++0x compiler would be wrong to reject it.
>
> I believe you are misunderstanding Francis' intention here. He is
> exactly referencing the expected rules for a C++03-compatible
> compiler here and any compiler that would declare itself as
> satisfying this standard has to make the OP's code ill-formed,
> whether the programmer likes it or not.
In this case the rule is hard enough ("not allowed except when it is
required") that the next standard is very likely to change the rule.
Some compilers are already nice enough to just issue a warning (the
required diagnostic) and continue the translation. In the future this
will be the norm, so that the OP's code will be acceptable.
Bo Persson
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]