🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Erik Oestlyngen Fri, 23 Feb 1996 19:45
Type conflicts
#3430
Author: Erik Oestlyngen
Date: Fri, 23 Feb 1996 19:45
64 lines
1899 bytes
Hi.

I'm doing working with a project trying to make a model for namespace
structure of C++ programs, and make a visualization of it. (by the
way, does anyone know of any interesting stuff on the net in this
context ?)

Anyhow, i've got this little problem:

When i define two different structs with the same name in two
different files, my compiler don't complain. I can then use the struct
as input parameter for a function in one file, and from the other send
the different struct into the function. The function will then get
an other input type than it expects.

What does the ansi-C++ standard say about this?


Example:
========

File1:

struct a {
 int x;
};

extern void f(struct a);

void main(void)
{
 struct a str;

 str.x=1;

 f(str);
}

---------------------

File2:

struct a {
 long x;
};

void f(struct a astr)
{
  astr.x+=2;    // crash, bang, crunch
}

-- 
--Erik Oestlyngen

---------------------------------------------------------------
erikoest@idt.unit.no      URL http://www.idt.unit.no/~erikoest/


[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Re: Type conflicts
#3432
Author: Steve Clamage
Date: Fri, 23 Feb 1996 22:38
26 lines
1629 bytes
In article fsf@pil.idt.unit.no, erikoest@idt.unit.no (Erik Oestlyngen) writes:
>
>When i define two different structs with the same name in two
>different files, my compiler don't complain. I can then use the struct
>as input parameter for a function in one file, and from the other send
>the different struct into the function. The function will then get
>an other input type than it expects.

>What does the ansi-C++ standard say about this?

The One-Definition Rule says in part that two types with the same name
in the same scope must have the same definition.

Your code violates the One-Definition Rule, and has undefined behavior.
The compiler is not required to diagnose the error.

---
Steve Clamage, stephen.clamage@eng.sun.com


[ To submit articles: Try just posting with your newsreader.
		      If that fails, use mailto:std-c++@ncar.ucar.edu
  FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads