0

For example, a.h

static inline void asdf(int a) {}

b.h

static inline void asdf(int a) {}

is this ok? will there be any conflicts?

Sato
  • 8,192
  • 17
  • 60
  • 115
  • 3
    Possible duplicate of [What's the difference between "static" and "static inline" function?](https://stackoverflow.com/questions/7762731/whats-the-difference-between-static-and-static-inline-function) – Déjà vu Nov 30 '17 at 06:28
  • 3
    Why not define it in a *single* header file that you include where the function is needed? At least you would avoid conflicts if you some day need to include both `a.h` and `b.h`. – Some programmer dude Nov 30 '17 at 06:28
  • There isn't any conflict unless both a.h and b.h are included in one cpp file. But it is a bad practice to implement static functions in header file. – iamnoten Nov 30 '17 at 06:28
  • `static` is `static`, the func might be inlined or no, that's another problem. – Déjà vu Nov 30 '17 at 06:29
  • 1
    @iamnoten: It is bad practice to implement `static` non-`inline` functions in a header. It is perfectly reasonable to implement `static inline` functions in a header — indeed, as long as the function is (small enough and simple enough to be) inlined, it is perfectly sensible. – Jonathan Leffler Nov 30 '17 at 07:09
  • @JonathanLeffler Got it. thanks. – iamnoten Nov 30 '17 at 07:12

3 Answers3

2

Technically, yes, it's ok. static functions are only visible in the same compilation unit.

Practically, no, it's not ok. It makes your code hard to read and if I had to maintain your code later I'd hate you for it. Either because the functions do different things or because they diverge over time because someone fixed a bug in one of them and forgot the other.

Art
  • 19,807
  • 1
  • 34
  • 60
1

A static inline function does not need a declaration to be included in the header file.

You can have the declaration in the .c file where it is used.

e.g. a.c

static inline void asdf(int a);

....
static inline void asdf(int a)
{
   ....
}

In such a case the two different functions can be used in two files.

Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
0

It will only be ok if no single source ever include both headers. If all .c files either include only a.h or only b.h or none, then all will be fine.

If both header were included in same source, you would get an error for redefinition of asdf.

An acceptable example would be if none of the .c files include directly a.h or b.h but only a c.h file containing:

#ifdef A_TYPE
#include a.h
#else
#include b.h
#endif

because the choice of the implementation would rely on a compile time constant

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252