bhaviour of front method on an empty map

  • Thread starter Abhishek Saksena
  • Start date
A

Abhishek Saksena

Hi all,
Can somebody help me what the bhaviour of front method on an empty map.
For some reason calling front on an empty map seems to work with gcc
compiler but not with .NET.

Abhishek
 
G

Gavin Deane

Abhishek said:
Hi all,
Can somebody help me what the bhaviour of front method on an empty map.
For some reason calling front on an empty map seems to work with gcc
compiler but not with .NET.

By map do you mean std::map? That class has no front member function.
This code will not compile for that reason:

#include <map>

int main()
{
std::map<int, int> m;
m.front();
}

If I've misunderstood you, post the code you're having trouble with
following the guidelines here
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Gavin Deane


Gavin Deane
 
S

Sumit Rajan

Abhishek Saksena said:
Hi all,
Can somebody help me what the bhaviour of front method on an empty map.
For some reason calling front on an empty map seems to work with gcc
compiler but not with .NET.

Calling front() or back() for an empty container is undefined behaviour.
Anything goes!

Regards,
Sumit.
 
S

Sumit Rajan

Sumit Rajan said:
Calling front() or back() for an empty container is undefined behaviour.
Anything goes!


However, as Gavin mentioned, there is not front() or back() in a map.

Regards,
Sumit.
 
D

Dietmar Kuehl

Abhishek said:
Can somebody help me what the bhaviour of front method on an empty map.
For some reason calling front on an empty map seems to work with gcc
compiler but not with .NET.

There is no method 'front()' in 'std::map<S, T>' according to the
standard. If the implementation(s) you are using have such a method,
they are environment specific extensions which can do whatever these
extensions choose to do.

For those containers which do have a 'front()' methods (e.g.
'std::vector<T>', 'std::deque<T>', 'std::list<T>') it is defined
to have the same effect as '*c.begin()'. This expression is only
defined if 'c.begin() != c.end()' which yields 'false' for an
empty container ('c.empty()' is defined to be 'c.size() == 0' and
'c.size()' in turn is defined as 'c.end() - c.begin()'). Thus, the
result is undefined behavior.
 
R

Rolf Magnus

Abhishek said:
Hi all,
Can somebody help me what the bhaviour of front method on an empty map.

Besides the fact that map doesn't even provide a member function front(),
what would you expect it to return? What is the first element of a
container that has no elements?
For some reason calling front on an empty map seems to work with gcc
compiler but not with .NET.

What do you mean by "seems to work"? AFAICS, calling front() for an empty
container results in undefined behaivor, and for me, gcc doesn't even
compile it
because std::map has no front() function.
 
D

Dietmar Kuehl

Abhishek said:
Sorry Guys! it is not a map but a std::vector

However, as noted before, 'v.front()' on an empty 'std::vector<T>'
has undefined behavior.
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top