Inline functions C++ bug?

R

Ruud van Gaal

Hi,

I'm probably wrong at this, but can't understand the following:

I have 2 separate modules, mod1.cpp and mod2.cpp:

--- mod1.cpp ---
#include <stdio.h>

void mod2();

class A
{
public:
void f()
{
printf("mod1:A:f\n");
}
};

void main()
{
A a;
a.f();
mod2();
}

--- mod2.cpp ---

#include <stdio.h>

class A
{
public:
void f()
{
printf("mod2:A:f\n");
}
};

void mod2()
{
A a;
a.f();
}

---

Notice the local class A which inlines its functions. When running
this, I get:

mod1:A:f
mod1:A:f

instead of my expected:

mod1:A:f
mod2:A:f

The mod2.cpp's A:f() function is ignored and mod1.cpp:A::f() is called
instead!? Anybody have an explanation?
I tried this on MSVC6, MSVC7.1 and g++4.0; all exhibit the same
behavior.

Thanks,
Ruud
 
V

Victor Bazarov

Ruud said:
I'm probably wrong at this, but can't understand the following:

I have 2 separate modules, mod1.cpp and mod2.cpp:

--- mod1.cpp ---
#include <stdio.h>

void mod2();

class A
{
public:
void f()
{
printf("mod1:A:f\n");
}
};

void main()

int main()
{
A a;
a.f();
mod2();
}

--- mod2.cpp ---

#include <stdio.h>

class A
{
public:
void f()
{
printf("mod2:A:f\n");
}
};

void mod2()
{
A a;
a.f();
}

---

Notice the local class A which inlines its functions. When running
this, I get:

mod1:A:f
mod1:A:f

instead of my expected:

mod1:A:f
mod2:A:f

The mod2.cpp's A:f() function is ignored and mod1.cpp:A::f() is called
instead!? Anybody have an explanation?

Your program has undefined behavior because two functions with the same
name have _different_ definitions in two different modules. Anything is
allowed to happen. So, your expectations of something particular are
unfounded.
I tried this on MSVC6, MSVC7.1 and g++4.0; all exhibit the same
behavior.

It's a coincidence.

V
 
A

Alf P. Steinbach

* Ruud van Gaal:
I'm probably wrong at this, but can't understand the following:

I have 2 separate modules, mod1.cpp and mod2.cpp:

--- mod1.cpp ---
#include <stdio.h>

void mod2();

class A
{
public:
void f()
{
printf("mod1:A:f\n");
}
};

void main()

That's not allowed by the standard.

E.g., with g++ 3.4.4 you would get:

oko.cpp:15: error: `main' must return `int'
oko.cpp:15: error: return type for `main' changed to `int'

{
A a;
a.f();
mod2();
}

--- mod2.cpp ---

#include <stdio.h>

class A
{
public:
void f()
{
printf("mod2:A:f\n");
}
};

void mod2()
{
A a;
a.f();
}

There is no local class A.

which inlines its functions. When running this, I get:

mod1:A:f
mod1:A:f

instead of my expected:

mod1:A:f
mod2:A:f

You have Undefined Behavior (namely, violating the one-definition rule for
class A, plus, invalid result type for main), and so can't expect anything.

The mod2.cpp's A:f() function is ignored and mod1.cpp:A::f() is called
instead!? Anybody have an explanation?
I tried this on MSVC6, MSVC7.1 and g++4.0;

I rather doubt that g++ 4.0 will compile your code when g++ 3.4.4 does not.
 
G

Greg

Ruud said:
Hi,

I'm probably wrong at this, but can't understand the following:

I have 2 separate modules, mod1.cpp and mod2.cpp:

--- mod1.cpp ---
#include <stdio.h>

void mod2();

class A
{
public:
void f()
{
printf("mod1:A:f\n");
}
};

void main()
{
A a;
a.f();
mod2();
}

--- mod2.cpp ---

#include <stdio.h>

class A
{
public:
void f()
{
printf("mod2:A:f\n");
}
};

void mod2()
{
A a;
a.f();
}

---

Notice the local class A which inlines its functions. When running
this, I get:

mod1:A:f
mod1:A:f

instead of my expected:

mod1:A:f
mod2:A:f

The mod2.cpp's A:f() function is ignored and mod1.cpp:A::f() is called
instead!? Anybody have an explanation?
I tried this on MSVC6, MSVC7.1 and g++4.0; all exhibit the same
behavior.

Ordinarily you would see a linker error owing to A::f's multiple
definitions. Unfortunately, since you have make A::f inline, the linker
will accept multiple definitions of A::f but assumes, and does not
verify, that the definitions are all the same (as they are required to
be).

If you want two different A classes, declare each in an unnamed
namespace. Doing so will allow the program to have file-specific
declarations of A. Declaaring A within a function is another way to
make it local.

Greg
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top