I'm curious as to why returning a reference to a local inside an inline function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage as you'd expect if it were a non inline. However surely if the function is truly inline it should work. Are locals for inlines temporaries created on the
heap instead of being stored on the same stack as the calling function locals?
I'm curious as to why returning a reference to a local inside an inline function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage as you'd expect if it were a non inline. However surely if the function is truly inline it should work. Are locals for inlines temporaries created on the
heap instead of being stored on the same stack as the calling function locals?
I'm curious as to why returning a reference to a local inside an inline function isn't (apparently) allowed. eg:
Muttley@dastardlyhq.com wrote:
I'm curious as to why returning a reference to a local inside an inline
function isn't (apparently) allowed. eg:
For all intents and purposes 'inline' (when used in a function definition)
is an instruction for the linker, not the compiler, and should really be thought as such.
In all likelihood the compiler is going to ignore the keyword when making inlinining decisions (I don't know if it has ever had any effect with any compiler).
In any case, it doesn't change the semantics of the function.
The function still has all the requirements of a "non-inline" function
(with the exception of the changed linking strategy). You should always
think of "inline" functions as if they weren't inlined (which is a real possibility, because the standard allows compilers to not actually do the inlining.)
'inline' is an instruction for the linker because it instructs the linker
to do something special with that function signature. (It tells it that
if it finds that function implementation in more than one compilation
unit, rather than give an error for duplicate symbols it should just
choose one of them and discard the rest.)
'inline' is an instruction for the linker because it instructs the linker
to do something special with that function signature. ...
I'm curious as to why returning a reference to a local inside an inline function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage as you'd expect if it were a non inline. However surely if the function is truly inline it should work. Are locals for inlines temporaries created on the
heap instead of being stored on the same stack as the calling function locals?
12.01.2022 18:34 Muttley@dastardlyhq.com kirjutas:
I'm curious as to why returning a reference to a local inside an inline
function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage >> as you'd expect if it were a non inline. However surely if the function is >> truly inline it should work. Are locals for inlines temporaries created on >the
heap instead of being stored on the same stack as the calling function >locals?
Declaring a function inline does not change the semantics of the
function. In particular, it must not change the lifetime of objects.
Imagine a RAII mutex lock like std::lock_guard, extending its lifetime >without programmer's consent might be disastrous.
I'm curious as to why returning a reference to a local inside an inline function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage as you'd expect if it were a non inline. However surely if the function is truly inline it should work.
Are locals for inlines temporaries created on the
heap instead of being stored on the same stack as the calling function locals?
Am 13.01.2022 um 08:00 schrieb Juha Nieminen:
'inline' is an instruction for the linker because it instructs the linker
to do something special with that function signature. ...
If you don't use link-time compiling the linker doesn't even see the inline-function.
On Thu, 13 Jan 2022 17:51:45 -0800
Andrey Tarasevich <andreyta...@hotmail.com> wrote:
On 1/12/2022 8:34 AM, Mut...@dastardlyhq.com wrote:
I'm curious as to why returning a reference to a local inside an inline
function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage
as you'd expect if it were a non inline. However surely if the function is >> truly inline it should work.
Um... You are seriously confused. This is a rather widespread
misconception that keyword `inline` has something to do with "inlining" >function code at the call site.
1. "Declaring function as inline" and "embedding (inlining) function
code at a specific call site" are two completely different, independent, >unrelated things. In your example you simply declared a function as Wikipedia says otherwise:
https://en.wikipedia.org/wiki/Inline_function
"It serves as a compiler directive that suggests (but does not require) that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function
call,"
Perhaps you're the one who's getting confused?
On 1/12/2022 8:34 AM, Muttley@dastardlyhq.com wrote:
I'm curious as to why returning a reference to a local inside an inline
function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage >> as you'd expect if it were a non inline. However surely if the function is >> truly inline it should work.
Um... You are seriously confused. This is a rather widespread
misconception that keyword `inline` has something to do with "inlining" >function code at the call site.
1. "Declaring function as inline" and "embedding (inlining) function
code at a specific call site" are two completely different, independent, >unrelated things. In your example you simply declared a function as
On Thu, 13 Jan 2022 17:51:45 -0800...
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
Um... You are seriously confused. This is a rather widespread
misconception that keyword `inline` has something to do with "inlining"
function code at the call site.
1. "Declaring function as inline" and "embedding (inlining) function
code at a specific call site" are two completely different, independent,
unrelated things. In your example you simply declared a function as
Wikipedia says otherwise:
https://en.wikipedia.org/wiki/Inline_function
"It serves as a compiler directive that suggests (but does not require) that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function
call,"
Perhaps you're the one who's getting confused?
On Thu, 13 Jan 2022 17:51:45 -0800
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
On 1/12/2022 8:34 AM, Muttley@dastardlyhq.com wrote:
I'm curious as to why returning a reference to a local inside an inline
function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage
as you'd expect if it were a non inline. However surely if the function is >>> truly inline it should work.
Um... You are seriously confused. This is a rather widespread
misconception that keyword `inline` has something to do with "inlining"
function code at the call site.
1. "Declaring function as inline" and "embedding (inlining) function
code at a specific call site" are two completely different, independent,
unrelated things. In your example you simply declared a function as
Wikipedia says otherwise:
https://en.wikipedia.org/wiki/Inline_function
"It serves as a compiler directive that suggests (but does not require) that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function
call,"
Perhaps you're the one who's getting confused?
standard's wording above makes "inline" no more than a hint. An implementation is free to perform inline substitution of any function
call, whether or not the function is declared 'inline', so long as the
result produces the same observable effects as calling a separate
function.
On Friday, 14 January 2022 at 13:22:31 UTC+2, Mut...@dastardlyhq.com wrote:
On Thu, 13 Jan 2022 17:51:45 -0800
Andrey Tarasevich <andreyta...@hotmail.com> wrote:
On 1/12/2022 8:34 AM, Mut...@dastardlyhq.com wrote:Wikipedia says otherwise:
I'm curious as to why returning a reference to a local inside an inline >> >> function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out >garbage
as you'd expect if it were a non inline. However surely if the function >is
truly inline it should work.
Um... You are seriously confused. This is a rather widespread
misconception that keyword `inline` has something to do with "inlining"
function code at the call site.
1. "Declaring function as inline" and "embedding (inlining) function
code at a specific call site" are two completely different, independent,
unrelated things. In your example you simply declared a function as
https://en.wikipedia.org/wiki/Inline_function
"It serves as a compiler directive that suggests (but does not require) that
the compiler substitute the body of the function inline by performing inline
expansion, i.e. by inserting the function code at the address of each >function
call,"
Perhaps you're the one who's getting confused?
Wikipedia is (in kind of populist/politician manner) saying very same thing >that Andrey said. Read the Wikipedia article carefully (and as whole).
Does the sentence you quoted say that functions that you have declared
inline will be inlined, ever?
On Friday, 14 January 2022 at 13:22:31 UTC+2, Mut...@dastardlyhq.com wrote:
On Thu, 13 Jan 2022 17:51:45 -0800
Andrey Tarasevich <andreyta...@hotmail.com> wrote:
On 1/12/2022 8:34 AM, Mut...@dastardlyhq.com wrote:Wikipedia says otherwise:
I'm curious as to why returning a reference to a local inside an inline >>>> function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out garbage
as you'd expect if it were a non inline. However surely if the function is >>>> truly inline it should work.
Um... You are seriously confused. This is a rather widespread
misconception that keyword `inline` has something to do with "inlining"
function code at the call site.
1. "Declaring function as inline" and "embedding (inlining) function
code at a specific call site" are two completely different, independent, >>> unrelated things. In your example you simply declared a function as
https://en.wikipedia.org/wiki/Inline_function
"It serves as a compiler directive that suggests (but does not require) that >> the compiler substitute the body of the function inline by performing inline >> expansion, i.e. by inserting the function code at the address of each function
call,"
Perhaps you're the one who's getting confused?
Wikipedia is (in kind of populist/politician manner) saying very same thing that Andrey said. Read the Wikipedia article carefully (and as whole).
Does the sentence you quoted say that functions that you have declared
inline will be inlined, ever?
On 1/14/2022 1:02 PM, Öö Tiib wrote:
On Friday, 14 January 2022 at 13:22:31 UTC+2, Mut...@dastardlyhq.com wrote: >>> On Thu, 13 Jan 2022 17:51:45 -0800
Andrey Tarasevich <andreyta...@hotmail.com> wrote:
On 1/12/2022 8:34 AM, Mut...@dastardlyhq.com wrote:
I'm curious as to why returning a reference to a local inside an inline >>>>> function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out >garbage
as you'd expect if it were a non inline. However surely if the function is
Wikipedia says otherwise:truly inline it should work.
Um... You are seriously confused. This is a rather widespread
misconception that keyword `inline` has something to do with "inlining" >>>> function code at the call site.
1. "Declaring function as inline" and "embedding (inlining) function
code at a specific call site" are two completely different, independent, >>>> unrelated things. In your example you simply declared a function as
https://en.wikipedia.org/wiki/Inline_function
"It serves as a compiler directive that suggests (but does not require) that
the compiler substitute the body of the function inline by performing inline
expansion, i.e. by inserting the function code at the address of each >function
call,"
Perhaps you're the one who's getting confused?
Wikipedia is (in kind of populist/politician manner) saying very same thing >> that Andrey said. Read the Wikipedia article carefully (and as whole).
Does the sentence you quoted say that functions that you have declared
inline will be inlined, ever?
Moreover, if someone wants to learn programming from Wikipedia, good
luck to them. But don't expect me to trust their code.
On 1/14/22 8:34 AM, James Kuyper wrote:
...
standard's wording above makes "inline" no more than a hint. An
implementation is free to perform inline substitution of any function
call, whether or not the function is declared 'inline', so long as the
result produces the same observable effects as calling a separate
function.
I wanted to point out that the opposite transformation is also
permitted: and implementation can take code out of the function where it
is written, and replace it with a call to a separate function created by
the implementation containing that code.
Why would an implementation do this? For the opposite of the reason it
would inline some code. In general, inlining trades off an increase in
the execution speed against increased code size. There are important exceptions: the inlined code could be smaller than the code that would
needed to implement the function call. Even if it isn't, inline
substitution could open up opportunities for optimization involving interactions between the code inside the inlined function, and code surrounding the function call. However, when neither of those cases
apply, each call to a function that gets inlined increases the size of
the generated code.
When I first mentioned this possibility, I had no idea whether any
existing compiler would do this, but I thought it unlikely. I was corresponding surprised by a response that identified a particular
compiler that actually did it. As I might have anticipated, that
compiler was cross-compiling for a memory-starved embedded system. Unfortunately, that was a decade ago, and I no longer remember any details.
What do you think "inserting the function code at the address of each function call" means?
On Fri, 14 Jan 2022 04:02:21 -0800 (PST)
=?UTF-8?B?w5bDtiBUaWli?= <oot...@hot.ee> wrote:
On Friday, 14 January 2022 at 13:22:31 UTC+2, Mut...@dastardlyhq.com wrote: >> On Thu, 13 Jan 2022 17:51:45 -0800
Andrey Tarasevich <andreyta...@hotmail.com> wrote:
On 1/12/2022 8:34 AM, Mut...@dastardlyhq.com wrote:Wikipedia says otherwise:
I'm curious as to why returning a reference to a local inside an inline >> >> function isn't (apparently) allowed. eg:
#include <iostream>
#include <string>
using namespace std;
inline string &func()
{
string s = "hello";
return s;
}
int main()
{
cout << func() << endl;
return 0;
}
This causes a compilation warning with clang and when run prints out >garbage
as you'd expect if it were a non inline. However surely if the function >is
truly inline it should work.
Um... You are seriously confused. This is a rather widespread
misconception that keyword `inline` has something to do with "inlining" >> >function code at the call site.
1. "Declaring function as inline" and "embedding (inlining) function
code at a specific call site" are two completely different, independent, >> >unrelated things. In your example you simply declared a function as
https://en.wikipedia.org/wiki/Inline_function
"It serves as a compiler directive that suggests (but does not require) that
the compiler substitute the body of the function inline by performing inline
expansion, i.e. by inserting the function code at the address of each >function
call,"
Perhaps you're the one who's getting confused?
Wikipedia is (in kind of populist/politician manner) saying very same thing >that Andrey said. Read the Wikipedia article carefully (and as whole).What do you think "inserting the function code at the address of each function call" means?
Does the sentence you quoted say that functions that you have declared >inline will be inlined, ever?
As a historical note, it is true that keyword `inline` was originally introduced specifically to "suggest" inlining of function bodies at call sites. But it quickly became clear that it is useless and unnecessary in
this role. As a consequence, its role was refocused to being a "ODR
defeater" - a completely different purpose.
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
As a historical note, it is true that keyword `inline` was originally
introduced specifically to "suggest" inlining of function bodies at call
sites. But it quickly became clear that it is useless and unnecessary in
this role. As a consequence, its role was refocused to being a "ODR
defeater" - a completely different purpose.
And as a side note, the C standard also added support for 'inline'
functions, except that it's a different and very weird version of it that
to this day I still don't fully understand. It's like an 'inline' that's *not* an "ODR defeater". (It's so confusing that the vast, vast majority
of C programmers just write "static inline" to get around the confusing part.)
On 17/01/2022 06:38, Juha Nieminen wrote:[...]
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
As a historical note, it is true that keyword `inline` was originally
introduced specifically to "suggest" inlining of function bodies at call >>> sites. But it quickly became clear that it is useless and unnecessary in >>> this role. As a consequence, its role was refocused to being a "ODR
defeater" - a completely different purpose.
And as a side note, the C standard also added support for 'inline'
functions, except that it's a different and very weird version of it that
to this day I still don't fully understand. It's like an 'inline' that's
*not* an "ODR defeater". (It's so confusing that the vast, vast majority
of C programmers just write "static inline" to get around the confusing
part.)
Yes, "inline" functions in C are a bit odd, and the semantics are not
the same as in C++. As you say, the most common usage (and the only way
I use it) is to write "static inline" for functions to replace what
might pre-C99 have been a function-like macro. The "inline" here is not actually very significant - any decent C compiler will make its own
decisions about actual inlining optimisations, so a plain "static"
function would have the same effect. To me, at least, the "static
inline" is a documentation of how you see the function and expect it to
be used.
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
As a historical note, it is true that keyword `inline` was originally
introduced specifically to "suggest" inlining of function bodies at call
sites. But it quickly became clear that it is useless and unnecessary in
this role. As a consequence, its role was refocused to being a "ODR
defeater" - a completely different purpose.
And as a side note, the C standard also added support for 'inline'
functions, except that it's a different and very weird version of it that
to this day I still don't fully understand. It's like an 'inline' that's *not* an "ODR defeater". (It's so confusing that the vast, vast majority
of C programmers just write "static inline" to get around the confusing part.)
On 1/16/2022 11:12 PM, David Brown wrote:
On 17/01/2022 06:38, Juha Nieminen wrote:[...]
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
As a historical note, it is true that keyword `inline` was originally
introduced specifically to "suggest" inlining of function bodies at
call
sites. But it quickly became clear that it is useless and
unnecessary in
this role. As a consequence, its role was refocused to being a "ODR
defeater" - a completely different purpose.
And as a side note, the C standard also added support for 'inline'
functions, except that it's a different and very weird version of it
that
to this day I still don't fully understand. It's like an 'inline' that's >>> *not* an "ODR defeater". (It's so confusing that the vast, vast majority >>> of C programmers just write "static inline" to get around the confusing
part.)
Yes, "inline" functions in C are a bit odd, and the semantics are not
the same as in C++. As you say, the most common usage (and the only way
I use it) is to write "static inline" for functions to replace what
might pre-C99 have been a function-like macro. The "inline" here is not
actually very significant - any decent C compiler will make its own
decisions about actual inlining optimisations, so a plain "static"
function would have the same effect. To me, at least, the "static
inline" is a documentation of how you see the function and expect it to
be used.
Imvvvho, using inline is almost akin to almost begging the compiler to actually inline the function. It certainly can say f-you, and give the programmer the proverbial middle finger at the same time! For some
reason it kind of makes me think of the register keyword...
Now, it would be funny if a compiler would output something like:
this function cannot be inlined, go ahead and try turning on link time optimization, and try again? ;^)
Yes, "inline" functions in C are a bit odd, and the semantics are not
the same as in C++. As you say, the most common usage (and the only way
I use it) is to write "static inline" for functions to replace what
might pre-C99 have been a function-like macro. The "inline" here is not actually very significant - any decent C compiler will make its own
decisions about actual inlining optimisations, so a plain "static"
function would have the same effect. To me, at least, the "static
inline" is a documentation of how you see the function and expect it to
be used.
On 1/16/2022 11:12 PM, David Brown wrote:
On 17/01/2022 06:38, Juha Nieminen wrote:[...]
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
As a historical note, it is true that keyword `inline` was originally
introduced specifically to "suggest" inlining of function bodies at
call
sites. But it quickly became clear that it is useless and
unnecessary in
this role. As a consequence, its role was refocused to being a "ODR
defeater" - a completely different purpose.
And as a side note, the C standard also added support for 'inline'
functions, except that it's a different and very weird version of it
that
to this day I still don't fully understand. It's like an 'inline' that's >>> *not* an "ODR defeater". (It's so confusing that the vast, vast majority >>> of C programmers just write "static inline" to get around the confusing
part.)
Yes, "inline" functions in C are a bit odd, and the semantics are not
the same as in C++. As you say, the most common usage (and the only way
I use it) is to write "static inline" for functions to replace what
might pre-C99 have been a function-like macro. The "inline" here is not
actually very significant - any decent C compiler will make its own
decisions about actual inlining optimisations, so a plain "static"
function would have the same effect. To me, at least, the "static
inline" is a documentation of how you see the function and expect it to
be used.
Imvvvho, using inline is almost akin to almost begging the compiler to actually inline the function. It certainly can say f-you, and give the programmer the proverbial middle finger at the same time! For some
reason it kind of makes me think of the register keyword...
Now, it would be funny if a compiler would output something like:
this function cannot be inlined, go ahead and try turning on link time optimization, and try again? ;^)
2. Keyword `inline` has absolutely nothing to do with embedding function
code at the call site. The purpose of `inline` keyword is to allow you
to defeat ODR restrictions for a function (or a variable) with external linkage. ...
Some years ago g++ treated `inline` as an almost absolute inlining
directive.
Alf P. Steinbach <alf.p.steinbach@gmail.com> wrote:
Some years ago g++ treated `inline` as an almost absolute inlining
directive.
Does anyone have a reference or documentation that shows that compilers
like gcc, clang, icc and Visual Studio behave differently with respect
to inlining a function depending on whether the function has been marked
as 'inline' or not?
For the longest time I have got the impression that if a compiler sees
the function definition from the call location, it uses a heuristic to determine whether it will inline the function or not, and this heuristic completely ignores whether the function has been marked as 'inline'.
However, I could well be wrong. Maybe they use different heuristics
depending on whether that keyword appears or not?
Am 14.01.2022 um 02:51 schrieb Andrey Tarasevich:
2. Keyword `inline` has absolutely nothing to do with embedding
function code at the call site. The purpose of `inline` keyword is to
allow you to defeat ODR restrictions for a function (or a variable)
with external linkage. ...
That's true for inline-variables but for inline-functions only parti-
tially. For inline functions inline is also a hint for the compiler
to inline the code in the calling code. But this is only a hint and
not mandantory.
Alf P. Steinbach <alf.p.steinbach@gmail.com> wrote:
Some years ago g++ treated `inline` as an almost absolute inlining
directive.
Does anyone have a reference or documentation that shows that compilers
like gcc, clang, icc and Visual Studio behave differently with respect
to inlining a function depending on whether the function has been marked
as 'inline' or not?
On 1/17/2022 4:55 AM, Juha Nieminen wrote:
Alf P. Steinbach <alf.p.steinbach@gmail.com> wrote:
Some years ago g++ treated `inline` as an almost absolute inlining
directive.
Does anyone have a reference or documentation that shows that compilers
like gcc, clang, icc and Visual Studio behave differently with respect
to inlining a function depending on whether the function has been marked
as 'inline' or not?
Depends on the compiler settings. "By default" GCC considers for
inlining only those functions that are explicitly declared `inline`.
In order to inline something else one'd need to enable `-finline-functions-called-once` (included in `-O1`),
`-finline-functions`, `-finline-small-functions` (included in `-O2`) and
so on.
Since as far as I remember you cannot have 'static' variables inside
an 'inline' function (unlike in C++),
Um... Once again "only a hint to inline and not mandantory" is
completely meaningless wording within the context of a formal document.
Such wording should be relegated to a footnote, to explain the etymology
of the keyword.
The proper wording describing the purpose and intent of `inline` should
say something along the lines of "the purpose of `inline` keyword is to
make the definition of a function with external linkage visible in all translation units without triggering an ODR violation".
On 1/16/2022 9:38 PM, Juha Nieminen wrote:
Andrey Tarasevich <andreytarasevich@hotmail.com> wrote:
As a historical note, it is true that keyword `inline` was
originally introduced specifically to "suggest" inlining of
function bodies at call sites. But it quickly became clear that
it is useless and unnecessary in this role. As a consequence,
its role was refocused to being a "ODR defeater" - a completely
different purpose.
And as a side note, the C standard also added support for
'inline' functions, except that it's a different and very weird
version of it that to this day I still don't fully understand.
It's like an 'inline' that's *not* an "ODR defeater". (It's so
confusing that the vast, vast majority of C programmers just
write "static inline" to get around the confusing part.)
Yes, you are right. `inline` is clearly an "ODR defeater" in C++,
but in C... not so much.
Firstly, `static inline` is a separate story. Most of the time
static` is all that's needed. In a quality compiler `static
inline` is always redundant. It is 100% equivalent to plain
`static`. There's never any tangible reason to use `static` and
`inline` together. [...]
On 1/17/22 4:50 AM, Juha Nieminen wrote:
...
Since as far as I remember you cannot have 'static' variables inside
an 'inline' function (unlike in C++),
That only applies to inline functions with external linkage, and only
for modifiable objects. The same is true of objects with thread storage duration. Such functions are also prohibited from containing a reference
to an identifier with internal linkage. (C standard 6.7.4p3)
A key difference between C and C++ that is relevant to this thread is
that, in C++, 'inline' is an ignorable hint that inline substitution
should be performed. In C, it's an ignorable hint that "that calls to
the function be as fast as possible.", with the method where by that
might be achieved being unspecified.
James Kuyper <james...@alumni.caltech.edu> writes:...
A key difference between C and C++ that is relevant to this thread isNote the sentence in the semantics portion of 6.7.4 of the C
that, in C++, 'inline' is an ignorable hint that inline substitution
should be performed. In C, it's an ignorable hint that "that calls to
the function be as fast as possible.", with the method where by that
might be achieved being unspecified.
standard that says
The extent to which such suggestions are effective is
implementation-defined.
So I think "implementation-defined" is more accurate than
"unspecified".
On 1/17/22 1:13 PM, Andrey Tarasevich wrote:
...
Um... Once again "only a hint to inline and not mandantory" is
completely meaningless wording within the context of a formal document.
Such wording should be relegated to a footnote, to explain the etymology
of the keyword.
You might disapprove of such a feature, but being a non-mandatory hint
is the primary purpose of this feature. It seems odd to me to mention
the primary purpose of a feature only in a footnote.
The proper wording describing the purpose and intent of `inline` should
say something along the lines of "the purpose of `inline` keyword is to
make the definition of a function with external linkage visible in all
translation units without triggering an ODR violation".
Could you show me how you would use inline for the purpose of violating
the ODR rules, where there's no more appropriate way to achieve the same objective? Allowing you do do so was certainly not the purpose of 'inline'.
Um... This is some rather strange wording: "use inline for the purpose
of violating the ODR rules". Where did you get this? ODR rules,
obviously, are aware of `inline` and inclusive of `inline`. Nobody's
talking about "violating" them here in any formal way.
What I'm referring to is rather obvious from the above discussion.
The primary purpose of the feature is this: the whole problem, the whole objective is provide us with a feature, that would let us write
unsigned foo = 42;
void bar()
{
}
in a header file and then include this file into multiple TUs.
(`static` might be seen as workaround for the function, but we don't
want that. External linkage is the point here. For whatever reason, we
want a function with unique address identity for the whole program.)
That is the prime purpose of `inline`. Has always been. All these
stories about the "hint" is just a mumbo-jumbo, which probably only
persists in standard text out of respect to someone who originally
introduced it.
On 1/17/2022 3:40 AM, Bonita Montero wrote:
Am 14.01.2022 um 02:51 schrieb Andrey Tarasevich:
2. Keyword `inline` has absolutely nothing to do with embedding
function code at the call site. The purpose of `inline` keyword is to
allow you to defeat ODR restrictions for a function (or a variable)
with external linkage. ...
That's true for inline-variables but for inline-functions only parti-
tially. For inline functions inline is also a hint for the compiler
to inline the code in the calling code. But this is only a hint and
not mandantory.
Um... Once again "only a hint to inline and not mandantory" is
completely meaningless wording within the context of a formal document.
Such wording should be relegated to a footnote, to explain the etymology
of the keyword.
Currently, this is essentially a defect in the standard, which only
serves to confuse people.
On 1/17/22 7:28 PM, Andrey Tarasevich wrote:
...
Um... This is some rather strange wording: "use inline for the purpose
of violating the ODR rules". Where did you get this? ODR rules,
obviously, are aware of `inline` and inclusive of `inline`. Nobody's
talking about "violating" them here in any formal way.
You're talking about 'inline' as a way of getting around those rules. If 'inline' didn't exist, then those rules obviously couldn't cite it as an exception, and what you're claiming is the primary purpose of 'inline'
would be a violation of those rules. That purpose is expressed by
inserting an exception for 'inline' into those rules.
What I'm referring to is rather obvious from the above discussion.
The primary purpose of the feature is this: the whole problem, the whole
objective is provide us with a feature, that would let us write
unsigned foo = 42;
void bar()
{
}
in a header file and then include this file into multiple TUs.
Declaring them with internal linkage would achieve the same benefit.
...
(`static` might be seen as workaround for the function, but we don't
want that. External linkage is the point here. For whatever reason, we
want a function with unique address identity for the whole program.)
Why? I can imagine unusual circumstances where that might be needed, but
I wouldn't expect it to be a common need.
Note that an inline function
is only required to have a unique address if it has external linkage or module linkage (9.2.7p6). If having a single unique address was the main purpose of 'inline', why would it even be permitted to declare an inline function with internal linkage?
You could have functions with internal
linkage if you don't need a unique address, and 'inline' functions, with inherently external linkage, if you do need a unique address. If that's
the primary purpose, why didn't they it that way?
You can start asking your "why?" questions now. "Why do they allow
this?" "Why is it legal?" To me the answer is natural and obvious: why
not? No harm done, no need to overcomplicate things. This has always
been one of the cornerstone principles in thois language's design.
On 1/18/22 2:32 PM, Andrey Tarasevich wrote:
...
You can start asking your "why?" questions now. "Why do they allow
this?" "Why is it legal?" To me the answer is natural and obvious: why
not? No harm done, no need to overcomplicate things. This has always
been one of the cornerstone principles in thois language's design.
When you're claiming that what it actually says in 9.2.1p2 is
irrelevant, and stuff that it says nowhere in 9.2.7 is actually the
primary purpose of the 'inline' specifier, then you're going to need
better arguments than those. Having confirmed that you don't have any,
I'm going to leave this discussion - it's not going anywhere.
On Monday, January 17, 2022 at 6:35:03 PM UTC-5, Tim Rentsch wrote:
James Kuyper <james...@alumni.caltech.edu> writes:
...
A key difference between C and C++ that is relevant to this thread is
that, in C++, 'inline' is an ignorable hint that inline substitution
should be performed. In C, it's an ignorable hint that "that calls to
the function be as fast as possible.", with the method where by that
might be achieved being unspecified.
Note the sentence in the semantics portion of 6.7.4 of the C
standard that says The extent to which such suggestions are
effective is implementation-defined. So I think
"implementation-defined" is more accurate than "unspecified".
"implementation-defined" behavior is unspecified behavior that an implementation is required to document, so both terms are correct,
but "implementation-defined" is more specific. However, the point I
was making was about what the standard failed to specify, so
"unspecified" was relevant - that an implementation is required
to document the behavior wasn't.
"james...@alumni.caltech.edu" <james...@alumni.caltech.edu> writes:
On Monday, January 17, 2022 at 6:35:03 PM UTC-5, Tim Rentsch wrote:
James Kuyper <james...@alumni.caltech.edu> writes:
...
A key difference between C and C++ that is relevant to this thread is
that, in C++, 'inline' is an ignorable hint that inline substitution
should be performed. In C, it's an ignorable hint that "that calls to
the function be as fast as possible.", with the method where by that
might be achieved being unspecified.
Note the sentence in the semantics portion of 6.7.4 of the C
standard that says The extent to which such suggestions are
effective is implementation-defined. So I think
"implementation-defined" is more accurate than "unspecified".
"implementation-defined" behavior is unspecified behavior that an implementation is required to document, so both terms are correct,On the contrary, it is quite relevant. The requirement of being
but "implementation-defined" is more specific. However, the point I
was making was about what the standard failed to specify, so
"unspecified" was relevant - that an implementation is required
to document the behavior wasn't.
documented means a developer can read the documentation and rely on
a particular behavior, and that is a huge difference.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 113 |
Nodes: | 8 (1 / 7) |
Uptime: | 33:45:32 |
Calls: | 2,498 |
Files: | 8,649 |
Messages: | 1,906,523 |