Do I have to repeat the inlined keyword on function implementation
Asked Answered
D

2

6

I always try to keep implementation outside of headers, so for templates and inlined functions, I usually do something like this


// File.h
inline bool foo()

#include "File.hpp"

// File.hpp

inline bool foo()
{
    return 1;
}

My question is, what does the C++ specification have to say about repeating the inline keyword for the actual implementation of the function? (as shown in this example)

I don't really want to do that as it gets messy with lots and lots of functions, and while my compiler doesn't complain, I wonder if the compiler still accepts the inline hint.

Anyone know?

Disclaimer answered 17/7, 2012 at 13:38 Comment(3)
A little off-topic, but you shouldn't really rely on inline keyword, because since the optimization, a function stated inline may not be one, and a "normal" function may be actually treated as an inline one.Deliladelilah
I'm aware that its only a hint, yeahDisclaimer
This is a very opinionated and C++-hostile source: yosefk.com/c++fqa/inline.html that basically claims the inline keyword is mostly there to tell the compiler to take some precautions to make it possible to inline the function (if it chooses to do so) without multiple definitions etc. This would make the keyword more an artefact of early C++ compiler implementations more than anything else.Royceroyd
H
4

I tend to put inline as far from the interface as possible since it is an implementation detail and not part of the interface. Hence: omit the first inline in the declaration. And only attach it to the function definition. For the inclusion of an hpp compiler scopes are irrelevant in respect to inline since the files are treated as concatenated. See also http://www.parashift.com/c++-faq/where-to-put-inline-keyword.html for a more detailed explanation.

Homochromous answered 17/7, 2012 at 13:51 Comment(0)
U
4

It's OK, but putting inline in the source file is even less of a hint, because the sources aren't generally visible to other translation units. If you implement the function outside the header, the compiler will probably not be able to inline it anyways.

The only practical use of inline, in my opinion, is to prevent multiple definition of functions defined in the header.

Untread answered 17/7, 2012 at 13:39 Comment(4)
I haven't put it in a source file, I've put it in a file which I import in the header, essentially just tucking it in at the end of that header fileDisclaimer
Couldn't a Sufficiently Clever Compiler inline the function at link time anyway?Royceroyd
I'm genuinely not too sure about that. Does anyone know how common that is?Disclaimer
@TomasCokis GCC has had LTO since about 4.5 it seems, but what exactly it's capable of doing is probably "documented" mainly in a bunch of mailing list threads.Royceroyd
H
4

I tend to put inline as far from the interface as possible since it is an implementation detail and not part of the interface. Hence: omit the first inline in the declaration. And only attach it to the function definition. For the inclusion of an hpp compiler scopes are irrelevant in respect to inline since the files are treated as concatenated. See also http://www.parashift.com/c++-faq/where-to-put-inline-keyword.html for a more detailed explanation.

Homochromous answered 17/7, 2012 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.