Inline functions C++ bug?

Discussion in 'C++' started by Ruud van Gaal, Oct 20, 2005.

  1. 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
     
    Ruud van Gaal, Oct 20, 2005
    #1
    1. Advertising

  2. Ruud van Gaal wrote:
    > 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
     
    Victor Bazarov, Oct 20, 2005
    #2
    1. Advertising

  3. * 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();
    > }
    >
    > ---
    >
    > Notice the local class A


    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.


    > all exhibit the same behavior.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Oct 20, 2005
    #3
  4. Ruud van Gaal

    Greg Guest

    Ruud van Gaal wrote:
    > 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
     
    Greg, Oct 21, 2005
    #4
    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. Abhi
    Replies:
    2
    Views:
    752
    E. Robert Tisdale
    Jul 3, 2003
  2. Replies:
    3
    Views:
    473
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    1,956
    Kevin Bracey
    Nov 21, 2003
  4. jamihuq

    converting inline functions to C functions

    jamihuq, May 16, 2006, in forum: C Programming
    Replies:
    7
    Views:
    354
  5. Rahul
    Replies:
    3
    Views:
    463
    James Kanze
    Feb 28, 2008
Loading...

Share This Page