473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

why this program can get compiled using g++ without any complain?

Hi,

I wrote a simple program to learn the usage of STL template
map and set. Although I did not give out any Compare function
with respect to struct A, the program got compiled without
any complain using g++. Could you explain why?

Many thanks!

#include <iostream>
#include <map>
#include <set>
using namespace std;

struct A{
int x, y;
};

int main()
{
A a1, a2;
a1.x = 5; a1.y = 6;
a2.x = 7; a2.y = 8;
map< pair<A*, A*, inttest;
set< pair<A*, A* haha;
test.insert( make_pair( make_pair(&a1, &a2), 6) );
haha.insert( make_pair(&a1, &a2) );
return 0;
}

Jul 26 '06 #1
3 1158
yuyang08 wrote:
map< pair<A*, A*, inttest;
(Tip: always use a typedef for non-trivial templated types.)

That map contains pointers to A. Pointers have built-in operators for ==, <,
>, etc.
Take out the two *s and see what happens.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jul 26 '06 #2
Phlip wrote:
yuyang08 wrote:
> map< pair<A*, A*, inttest;

(Tip: always use a typedef for non-trivial templated types.)

That map contains pointers to A. Pointers have built-in operators for ==,
<,
>, etc.
That is not entirely correct: although operator< is syntactically
well-formed for arguments of type A*, this comparison is

(a) only defined for pointers into an array of A, and
(b) not used in the comparison for the map above.

The map uses std::less<>, and std::less< pair<S,T is defined in terms of
std::less<Sand std::less<T>. It so happens that the standard requires
std::less<A*to be well-defined and an ordering for all possible values of
A*. This requirement, however, is totally unrelated to operator< as applied
to A*: there is no guarantee that std::less<A*will call operator<(A*,A*).
Best

Kai-Uwe Bux
Jul 26 '06 #3
yu******@gmail.com wrote:
...
I wrote a simple program to learn the usage of STL template
map and set. Although I did not give out any Compare function
with respect to struct A, the program got compiled without
any complain using g++. Could you explain why?
You program does not depend in any way on the comparison function for 'struct
A'. That's why the compiler does not complain.

Your containers use 'std::pair<A*, A*>' values as keys. That's what needs to be
compared. Standard library provides a template that implements comparison
function 'operator <' for 'std::pair<>' specializations. That template will in
turn rely on the 'operator <' for 'A*' values. The latter is built-in in the
language.

If you want your compiler to complain, try using

set< A haha1;

or

set< pair<A, A haha2;

Both will require a comparison function for 'struct A' and, since you didn't
provide one, the compiler will complain.
>
#include <iostream>
#include <map>
#include <set>
using namespace std;

struct A{
int x, y;
};

int main()
{
A a1, a2;
a1.x = 5; a1.y = 6;
a2.x = 7; a2.y = 8;
map< pair<A*, A*, inttest;
set< pair<A*, A* haha;
test.insert( make_pair( make_pair(&a1, &a2), 6) );
haha.insert( make_pair(&a1, &a2) );
return 0;
}
--
Best regards,
Andrey Tarasevich
Jul 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Juho Saarikko | last post by:
The program attached to this message makes the Python interpreter segfault randomly. I have tried both Python 2.2 which came with Debian Stable, and self-compiled Python 2.3.3 (newest I could find...
3
by: David | last post by:
i am trying to compile the following with gcc on a RH 9.0 box: #include <sys/types.h> #include <sys/resource.h> #include <sys/time.h> #include <unistd.h> #include <stdio.h> /* resource.c */
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
12
by: kim | last post by:
The c program is an example and should be correct. I tried to compile the c file with MS studio 6.0. But failed with the several err and warning messages. Does anyone know how to make it work?...
18
by: SSG | last post by:
How to write a object oriented program in c? give me one example....
12
by: elty123 | last post by:
I have a small C# program (about 400 lines of code) that is only 28kb after compiled. However when it runs (takes a whole 5 seconds) it takes up nearly 20MB of memory and I don't see why. ...
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
38
by: abasili | last post by:
Hi everyone, I'm trying to compile a C++ function and then call it from a C program. Since Google is my friend I've ended up to this link which seems very clear: ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.