A
Andre Kostur
JKop said:But didn't some-one say that inline functions are allowed a multiple
definition?
Read section 3.2(5) of the Standard. Of particular interest is that
"inline function with external linkage" is permitted to have multiple
definitions, with certain restrictions, the most important in this case
is "each definition of D shall consist of the same sequence of tokens".
Your example violates that condition, thus you have undefined behaviour.
Is a C++ Standard compliant compiler allowed to compile whatever it
likes? For instance can it compile the following without giving any
errors at all?:
Not quite. There are certain things that require diagnostics and some
that don't.
#include <int main()
{
#include <iostream> chocolate;
++chocolate++ = 54;
return inline; ? 6 :? cout << "Hello World";>
I'd get a different compiler if it did that without a diagnostic.. but
that's a Quality of Implementation issue, not a Standard issue.
If so, then yes, my sample code does in fact have a Multiple
Definition. Deductive Reasoning:
1) Multiple Definitions are Undefined Behaviour.
Imprecise definitions. Depends on what you mean by "Multiple
Definitions". If you have the _same_ sequence of tokens for an inline
with external linkage in two different translation units, is that a
"Multiple Definition"? If so, then this premise is not universally true.
2) My code contains Multiple Definitions.
Yes
3) My code contains Undefined Behaviour.
Yes
What I would absolutely love an explanation to is this, why does the
following not compile:
int Yum(char)
{
std::cout << "monkey!";
return 't';
}
int Yum(char)
{
std::cout << "ape!";
return 'k';
}
Those two are simply functions (which falls under Section 3.2(3))
while the following does:
inline int Yum(char)
{
std::cout << "monkey!";
return 't';
}
inline int Yum(char)
{
std::cout << "ape!";
return 'k';
}
These are inline functions with external linkage (which falls under
Section 3.2(5)).
Is it:
A) Because Standard C++ allows multiple definitions of inline
functions. If so, then my code is *not* undefined behaviour, as
there's nothing undefined about multiple defining an inline function.
Depending on your definition of "multiple definitions". Multple places
where you specify the body of the function? Yep it's allowed. As long
as the two bodies are the same sequence of tokens.
B) Because g++ is shit?
You seem to be fixated on something.....