Is this right??Is redefine??

R

redriver

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()"?
 
C

cbmanica

redriver said:
/////////////////////////////////////////////////////////////////////////////////////////////////
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.
 
V

Victor Bazarov

redriver said:
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
 
C

Christopher Benson-Manica

Victor Bazarov said:
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.
 
V

Victor Bazarov

Christopher said:
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
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top