Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question on map< list<T>::iterator, int>

Reply
Thread Tools

question on map< list<T>::iterator, int>

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      05-03-2008
Consider the program x.cpp :

#include <cstdlib>
#include <iostream>
#include <list>
#include <map>

using namespace std;

int main()
{
map<list<int>::iterator, int> m;

list<int> c;
c.push_back(100);

// m[c.begin()] = 10;

return EXIT_SUCCESS;
}

This program compiles fine with the command
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

However if I remove the comment, I get compilation error because
operator '<' is not defined for list<T>::iterator. Why doesn't the
compiler give this error for
'map<list<int>::iterator, int> m;' itself. That is, in this first
statement inside main() itself, the compiler can know that the less-
than operator cannot be defined for the key type. So, why does the
compiler wait for an element to be pushed on to the map to flag the
error ?

Kindly clarify

Thanks
V.Subramanian
 
Reply With Quote
 
 
 
 
David Côme
Guest
Posts: n/a
 
      05-03-2008
On Sat, 03 May 2008 14:48:01 +0200, http://www.velocityreviews.com/forums/(E-Mail Removed), India
<(E-Mail Removed)> wrote:

> Consider the program x.cpp :
>
> #include <cstdlib>
> #include <iostream>
> #include <list>
> #include <map>
>
> using namespace std;
>
> int main()
> {
> map<list<int>::iterator, int> m;
>
> list<int> c;
> c.push_back(100);
>
> // m[c.begin()] = 10;
>
> return EXIT_SUCCESS;
> }
>
> This program compiles fine with the command
> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
>
> However if I remove the comment, I get compilation error because
> operator '<' is not defined for list<T>::iterator. Why doesn't the
> compiler give this error for
> 'map<list<int>::iterator, int> m;' itself. That is, in this first
> statement inside main() itself, the compiler can know that the less-
> than operator cannot be defined for the key type. So, why does the
> compiler wait for an element to be pushed on to the map to flag the
> error ?
>
> Kindly clarify
>
> Thanks
> V.Subramanian


A map is alway sorted by <.
To use an objet like a key for a std::map, these objet must have defined
operator<.
 
Reply With Quote
 
 
 
 
Paul Brettschneider
Guest
Posts: n/a
 
      05-03-2008
(E-Mail Removed), India wrote:

> [example code snipped]
> However if I remove the comment, I get compilation error because
> operator '<' is not defined for list<T>::iterator. Why doesn't the
> compiler give this error for
> 'map<list<int>::iterator, int> m;' itself. That is, in this first
> statement inside main() itself, the compiler can know that the less-
> than operator cannot be defined for the key type. So, why does the
> compiler wait for an element to be pushed on to the map to flag the
> error ?


This was discussed not too long ago in a thread named
"Inserting objects into a std::map?":

http://groups.google.com/group/comp....0d5cd88?lnk=st

When you create a map without ever inserting, why *should* the compiler
complain?
 
Reply With Quote
 
andrew.smith.cpp@gmail.com
Guest
Posts: n/a
 
      05-04-2008

>
> This program compiles fine with the command
> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
>

Why We use These Flags.?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-04-2008
(E-Mail Removed) wrote:
>> This program compiles fine with the command
>> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
>>

> Why We use These Flags.?
>

To get a reasonable level of warnings.

--
Ian Collins.
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      05-04-2008
On 2008-05-04 07:43, (E-Mail Removed) wrote:
>>
>> This program compiles fine with the command
>> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
>>

> Why We use These Flags.?



-std=c++98 means that we want to compile the code using the C++ 98
standard, -pedantic enables warnings for everything that should produce
a warning and rejects usages of private extansions. -Wall and -Wextra
turns on lots of warnings in case you do something dubious. If your code
compiles without producing any warnings you can be quite sure you have
not made anything obviously bad, though there are a lot of things that
the complier can not check for you.

--
Erik Wikström
 
Reply With Quote
 
andrew.smith.cpp@gmail.com
Guest
Posts: n/a
 
      05-05-2008
On May 4, 2:07 pm, Erik Wikstrm <(E-Mail Removed)> wrote:
> On 2008-05-04 07:43, (E-Mail Removed) wrote:
>
>
>
> >> This program compiles fine with the command
> >> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

>
> > Why We use These Flags.?

>
> -std=c++98 means that we want to compile the code using the C++ 98
> standard, -pedantic enables warnings for everything that should produce
> a warning and rejects usages of private extansions. -Wall and -Wextra
> turns on lots of warnings in case you do something dubious. If your code
> compiles without producing any warnings you can be quite sure you have
> not made anything obviously bad, though there are a lot of things that
> the complier can not check for you.
>
> --
> Erik Wikstrm


Thanks A Lot Erik
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-05-2008
On May 3, 2:48 pm, "(E-Mail Removed), India"
<(E-Mail Removed)> wrote:
> Consider the program x.cpp :


> #include <cstdlib>
> #include <iostream>
> #include <list>
> #include <map>


> using namespace std;


> int main()
> {
> map<list<int>::iterator, int> m;


> list<int> c;
> c.push_back(100);


> // m[c.begin()] = 10;


> return EXIT_SUCCESS;
> }


> This program compiles fine with the command
> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp


> However if I remove the comment, I get compilation error because
> operator '<' is not defined for list<T>::iterator. Why doesn't the
> compiler give this error for
> 'map<list<int>::iterator, int> m;' itself.


G++ does, if you give it the proper options. Try adding
-D_GLIBCXX_CONCEPT_CHECKS, for example. (This is with g++ 4.1.
I've heard that the option is not necessary with some later
versions, or at least with some builds of some later versions.)
Not providing the ordering relationship is undefined behavior,
howver, and a compiler is not required to diagnose it. Not all
do.

> That is, in this first statement inside main() itself, the
> compiler can know that the less- than operator cannot be
> defined for the key type. So, why does the compiler wait for
> an element to be pushed on to the map to flag the error ?


Because it can? The standard doesn't require a diagnostic.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-05-2008
On May 4, 11:07 am, Erik Wikstrm <(E-Mail Removed)> wrote:
> On 2008-05-04 07:43, (E-Mail Removed) wrote:


> >> This program compiles fine with the command
> >> g++ -std=c++98 -pedantic -Wall -Wextra x.cpp


> > Why We use These Flags.?


> -std=c++98 means that we want to compile the code using the
> C++ 98 standard, -pedantic enables warnings for everything
> that should produce a warning and rejects usages of private
> extansions.


Not entirely. It does turn off some very portable "future"
features, like long long, but it leaves some g++ extensions
active, including cases which render the compiler non-conformant
active.

> -Wall and -Wextra turns on lots of warnings in case you do
> something dubious.


And some which aren't dubious at all.

More to the point: if you're just learning C++, you should
probably invoke it however your instructor says to invoke it,
without worrying too much about why. Otherwise, you should
definitely read the documentation, and understand what each
option does, and why you want it, or not. For a variety of
reasons, all compilers require a fairly long list of options in
practice---200 or 300 hundred characters seems to be about a
minimum. (I need long long in my work, for example, so I can't
use -pedantic. But I can turn on most of what -pedantic
encompasses individually.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
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 row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments