I'd like to write a C++ library which is not-header-only by default but could be used as a header only library defining a NOLIB macro.

I've seen two approaches:

  • inline definitions

foo.h

#if !defined(FOO_H)
#define      FOO_H

#if defined(NOLIB)
#  define MYINLINE inline
#else 
#  define MYINLINE 
#endif

class foo
{
  // ...
};

#if defined(NOLIB)
#  include "foo.cc"
#endif

#endif  // include guard

foo.cc

#if !defined(NOLIB)
#  include "foo.h"
#endif

MYINLINE void foo::something() { ... }

  • "Artificial" template

foo.h

#if !defined(FOO_H)
#define      FOO_H

#if defined(NOLIB)
#  define MYTEMPLATE template<bool DUMMY>
#  define MYFOO      foo_impl
#  define MYFOO_T    foo_impl<DUMMY>
#else
#  define MYTEMPLATE
#  define MYFOO      foo
#  define MYFOO_T    foo
#endif

MYTEMPLATE
class MYFOO
{
  // ...
};

#if defined(NOLIB)
   using foo = foo_impl<true>;
#  include "foo.cc"
#endif

#endif  // include guard

foo.cc

#if !defined(NOLIB)
#  include "foo.h"
#endif

MYTEMPLATE
void MYFOO_T::something() { ... }

What are the pros and cons of these approaches? Are there better options?

有帮助吗?

解决方案

There is no real difference in each approach because both inline methods or templates could end up be inserted inline with your code depending on compiler optimizations. See this post discussing inline vs templates.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top