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

  • Thread starter subramanian100in
  • Start date
S

subramanian100in

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
 
D

David Côme

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<.
 
P

Paul Brettschneider

[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...read/5886d0e5eb5c4a19/a7cf8cbe50d5cd88?lnk=st

When you create a map without ever inserting, why *should* the compiler
complain?
 
E

Erik Wikström

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.
 
A

andrew.smith.cpp

-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.

Thanks A Lot Erik :)
 
J

James Kanze

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.
 
J

James Kanze

On 2008-05-04 07:43, (e-mail address removed) wrote:
-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.)
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top