Is this right??Is redefine??

Discussion in 'C++' started by redriver, May 10, 2006.

  1. redriver

    redriver Guest

    Because my english is very poor,so i give my program as follow:
    ////////////////////////////////////////////////////////////////////////////////////
    one.hpp:
    #include <iostream>
    #ifndef TEST
    #define TEST "The first print()"
    inline void print()
    {
    std::cout<<TEST<<std::endl;
    }

    #else
    #undef TEST
    #define TEST "the second print()"
    inline void print()
    {
    std::cout<<TEST<<std::endl;
    }
    #endif /*TEST*/
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    two.hpp:
    #define TEST "none"
    /////////////////////////////////////////////////////////////////////////////////////////////////
    unit1.cpp:
    #include "one.hpp"
    #include "two.hpp" //attention about sequence of two headers
    void doSomething()
    {
    print();
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    unit2.cpp
    #include "two.hpp"
    #include "one.hpp" //attention about sequence of two headers
    int main()
    {
    print();
    }

    what we can see there are two "print()"defined,and all in the globe
    namespace and
    why there is no error about redefine "print()"?
     
    redriver, May 10, 2006
    #1
    1. Advertising

  2. redriver

    Guest

    redriver wrote:
    > /////////////////////////////////////////////////////////////////////////////////////////////////
    > unit1.cpp:
    > #include "one.hpp"
    > #include "two.hpp" //attention about sequence of two headers


    > ///////////////////////////////////////////////////////////////////////////////////////////////////
    > unit2.cpp
    > #include "two.hpp"
    > #include "one.hpp" //attention about sequence of two headers


    There's no issue with building either of these two units - print() only
    gets defined once in each. If you link the units together to build an
    executable, yes, you are doing something bad, but whether or not the
    linker tells you this is a) up to the linker, depending on what options
    you give it, and b) off-topic for this group.
     
    , May 10, 2006
    #2
    1. Advertising

  3. redriver wrote:
    > Because my english is very poor,so i give my program as follow:
    > ////////////////////////////////////////////////////////////////////////////////////
    > one.hpp:
    > #include <iostream>
    > #ifndef TEST
    > #define TEST "The first print()"
    > inline void print()
    > {
    > std::cout<<TEST<<std::endl;
    > }
    >
    > #else
    > #undef TEST
    > #define TEST "the second print()"
    > inline void print()
    > {
    > std::cout<<TEST<<std::endl;
    > }
    > #endif /*TEST*/
    > ///////////////////////////////////////////////////////////////////////////////////////////////////
    > two.hpp:
    > #define TEST "none"
    > /////////////////////////////////////////////////////////////////////////////////////////////////
    > unit1.cpp:
    > #include "one.hpp"
    > #include "two.hpp" //attention about sequence of two headers
    > void doSomething()
    > {
    > print();
    > }
    > ///////////////////////////////////////////////////////////////////////////////////////////////////
    > unit2.cpp
    > #include "two.hpp"
    > #include "one.hpp" //attention about sequence of two headers
    > int main()
    > {
    > print();
    > }
    >
    > what we can see there are two "print()"defined,and all in the globe
    > namespace and
    > why there is no error about redefine "print()"?


    First of all, they are declared 'inline', so the compiler has to make
    sure it "merges" all definitions of it into one (if it decides to create
    an "out-of-line" definition, that is). The compiler is unable (or maybe
    unwilling) to check that your functions have different bodies. It is
    a violation of the One Definition Rule, but sometimes violations like
    this are subtle and you yourself need to watch for them.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 10, 2006
    #3
  4. Victor Bazarov <> wrote:

    > First of all, they are declared 'inline', so the compiler has to make
    > sure it "merges" all definitions of it into one (if it decides to create
    > an "out-of-line" definition, that is). The compiler is unable (or maybe
    > unwilling) to check that your functions have different bodies. It is
    > a violation of the One Definition Rule, but sometimes violations like
    > this are subtle and you yourself need to watch for them.


    I admit I didn't catch the subtleties involved with the use of
    "inline". OP's code is still wrong without inline, though, correct?

    I suppose the use of "inline" is what led to the linker being quiet;
    in my experience, this situation is reported by the linker assuming it
    is invoked with reasonable arguments.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, May 12, 2006
    #4
  5. Christopher Benson-Manica wrote:
    > Victor Bazarov <> wrote:
    >
    >> First of all, they are declared 'inline', so the compiler has to make
    >> sure it "merges" all definitions of it into one (if it decides to
    >> create an "out-of-line" definition, that is). The compiler is
    >> unable (or maybe unwilling) to check that your functions have
    >> different bodies. It is
    >> a violation of the One Definition Rule, but sometimes violations like
    >> this are subtle and you yourself need to watch for them.

    >
    > I admit I didn't catch the subtleties involved with the use of
    > "inline". OP's code is still wrong without inline, though, correct?


    Theoretically. The bodies of the two functions are different. That goes
    against the ODR. I don't know of any compiler that would enforce that,
    however.

    > I suppose the use of "inline" is what led to the linker being quiet;
    > in my experience, this situation is reported by the linker assuming it
    > is invoked with reasonable arguments.


    Then your linker if more advanced than the ones I've seen.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 12, 2006
    #5
    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. Michael Bane
    Replies:
    0
    Views:
    609
    Michael Bane
    Sep 11, 2003
  2. Fernando Martins

    Redefine entities in XSL.

    Fernando Martins, Oct 1, 2003, in forum: XML
    Replies:
    0
    Views:
    467
    Fernando Martins
    Oct 1, 2003
  3. BillD
    Replies:
    2
    Views:
    843
    BillD
    Oct 4, 2004
  4. Cat
    Replies:
    1
    Views:
    562
  5. Nafai
    Replies:
    4
    Views:
    351
    James Rafter
    Dec 8, 2004
Loading...

Share This Page