Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

[style] one-line functions in C

Name: Anonymous 2010-04-11 13:03


int frozen(int arg)
{
  return (arg+23);
}


OR


int frozen(int arg)
{ return (arg+23); }

int frozen(int arg)
  { return (arg+23); }


OR MAYBE EVEN SCREEN ESTATE SAVING


int frozen(int arg) { return (arg+23); }


The first version would be consistent myth my usual style, but version 2.2 looks too sweet to resist.

Name: Anonymous 2010-04-11 17:26

int frozen(int arg) {return arg + 23;}

>>5
Agreed. You should generally not worry about this at all during development because it's an optimization step that a) has hard to predict results (making even small functions inline might significantly worsen performance), and b) is extremely easy to change at the end of the development cycle (just move the dang thing to the header file and put 'static inline' in front.) The best solution is of course to let the compiler do it for you; use PGO and LTO where available (and turn off default inline for class scope functions) so you never have to think about manually inlining anything.

Inlining too much can also cause excessive header file dependencies; if an inline function calls another function, you have to move its include to your own header file, causing more dependencies and longer rebuild cycles. If in doubt, don't inline.

That being said, when writing a function like the above, I will put it in the header file as static inline upon writing, since a) the current platforms I'm targeting don't support LTO, b) it's obvious that optimizes down to a single processor instruction on almost all architectures, c) it doesn't require any header file dependencies, and d) it's typesafe (as opposed to a macro).

Name: Anonymous 2010-04-11 17:46

>>24
move the dang thing to the header file
in the header file as static inline
Are you mental?

Name: Anonymous 2010-04-11 18:11

>>23
I see you've thought this through!

Name: Anonymous 2010-04-11 18:12

I regret making this thread.

Name: Anonymous 2010-04-11 18:18

>>25
Why would >>24-san be mental?

Name: Anonymous 2010-04-11 18:18

>>27

It's ok, just confess your sins to Xarn and all will be forgiven.

Name: Anonymous 2010-04-11 18:26

>>28
By putting actual code in header files. Twice.

Name: Anonymous 2010-04-11 18:31

>>25,30
static inline. Where the fuck else do you put an inline function??

Name: Anonymous 2010-04-11 18:36

>>31
static inline. In a header. Why bother?

Name: Anonymous 2010-04-11 18:48

>>24,28,31
Read K&R.

Name: Anonymous 2010-04-11 18:49

>>30
So you're so hardcore that you copy and paste every inline function into every source file it is used in?

Name: Anonymous 2010-04-11 18:55

I don't know who the trolls are anymore.

Name: Anonymous 2010-04-11 19:00

>>35
Happens to the best of us.

Name: Anonymous 2010-04-11 19:54

>>35
Use the checklist:

Troll Checklist
If all boxes are checked at the end of the examination, you are dealing with a troll
[ ] They posted on /prog/

Name: Anonymous 2010-04-11 21:13

[✓] They posted on /prog/
Let me check here, one tick. Aww, dammit! IHBT

Name: Anonymous 2010-04-11 21:59

>>35
Anonymous is the troll.

Name: Anonymous 2010-04-12 4:47

I can't tell who's trolling who. Where does one place an inline function if not in a header file?

And static inline is necessary because gnu99 differs from c99 which also differs from c++ in the behaviour of bare inline. In gnu99, bare inline emits a non-inline function definition. In c99 it does not. In c++ it emits a static function definition.

Instead of trying to preproc all this bullshit, it's better to simply mark it static, and just don't take the address of an inline function. Most linkers are able to merge these definitions anyway (c++ linkers generally have to in order to avoid generating gigantic executables).

Name: Anonymous 2010-04-12 4:51

>>41
Sorry, that should read gnu89. Apparently gcc fixed the behaviour in gnu99.

I don't know how clang handles it but I assume it matches the spec, since it's one of the most pedantic compilers out there. Visual studio also differs from both gcc's c modes; it doesn't support inline in c, but using __inline emits as static inline iirc.

To sum it all up, not using static inline is going to break at some point on something somewhere.

Name: Anonymous 2010-04-12 5:50

#define i int
#define rt return
#define fr frozen
#define ar arg
i fr(i ar){rt ar+23;}

Name: Anonymous 2010-04-12 6:12

This ITT thread is full of COBOL-fags plus some ``Me is smarter than ur compiler''-pussies. Great!

Name: Anonymous 2010-04-12 6:16

>>44
U MENA JAVA

Name: GvR 2010-04-12 6:17

>>1
Curly braces are evil, and now you see precisely why.

def frozen(arg):
    return arg + 23

Name: Anonymous 2010-04-12 8:02

>>46
frozen = (23).__add__

Name: Anonymous 2010-04-12 9:25

>>47
Beautiful, but that's 23+add, not add+23.

Name: Anonymous 2010-04-12 10:31

Name: Anonymous 2010-04-12 12:10

ups, i haxed their servers xd

Name: Anonymous 2010-12-09 19:11

Name: Anonymous 2010-12-21 0:57

Name: Anonymous 2013-01-19 14:41

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List