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

Discussion in 'C++' started by subramanian100in@yahoo.com, India, May 3, 2008.

  1. , India

    , India Guest

    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
     
    , India, May 3, 2008
    #1
    1. Advertising

  2. On Sat, 03 May 2008 14:48:01 +0200, , India
    <> 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<.
     
    David Côme, May 3, 2008
    #2
    1. Advertising

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

    When you create a map without ever inserting, why *should* the compiler
    complain?
     
    Paul Brettschneider, May 3, 2008
    #3
  4. , India

    Guest


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

    Why We use These Flags.?
     
    , May 4, 2008
    #4
  5. , India

    Ian Collins Guest

    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.
     
    Ian Collins, May 4, 2008
    #5
  6. On 2008-05-04 07:43, 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
     
    Erik Wikström, May 4, 2008
    #6
  7. , India

    Guest

    On May 4, 2:07 pm, Erik Wikström <> wrote:
    > On 2008-05-04 07:43, 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


    Thanks A Lot Erik :)
     
    , May 5, 2008
    #7
  8. , India

    James Kanze Guest

    On May 3, 2:48 pm, ", India"
    <> 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 5, 2008
    #8
  9. , India

    James Kanze Guest

    On May 4, 11:07 am, Erik Wikström <> wrote:
    > On 2008-05-04 07:43, 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:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, May 5, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,247
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,697
    Old Wolf
    Jan 20, 2004
  3. Replies:
    6
    Views:
    683
    Jim Langston
    Oct 30, 2005
  4. Angus
    Replies:
    3
    Views:
    351
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    154
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page