mfontanini / cppkafka

Modern C++ Apache Kafka client library (wrapper for librdkafka)
BSD 2-Clause "Simplified" License
582 stars 207 forks source link

how to use it without boost? #110

Open Jenuce opened 5 years ago

Jenuce commented 5 years ago

boost is very big, why not do not depend on it?

mfontanini commented 5 years ago

I guess given C++17 has std::any and std::optional which are the 2 things currently being used by cppkafka, we could optionally allow using these features from namespace std rather than boost, but you'll need access to C++17 (or later).

accelerated commented 5 years ago

it could be opted out at compile time with

#if (__cplusplus >= 201703L)
... use std
#else 
... boost
#endif

I actually think this would be a good PR

mfontanini commented 5 years ago

This should actually go into the config.h file so that if you build the library, then you must use whatever you used while building. Otherwise you could build cppkafka using some compiler on C++14 (using boost), then you build your application using C++17 and link against the C++14 .so and then your code will go kaboom.

accelerated commented 5 years ago

You can actually catch this error at link time and prevent linking to mismatched libraries even w/o a config file or cmake flags.

//in the public cppkafka .h file
#if (__cpluplus >= 201703L)
extern int CPPKAFKA_COMPILED_STL;
static const int MY_LIBRARY_VERSION = CPPKAFKA_COMPILED_STL;
... stl include headers
#else
extern int CPPKAFKA_COMPILED_BOOST;
static const int MY_LIBRARY_VERSION = CPPKAFKA_COMPILED_BOOST;
... boost include headers
#endif

// inside any cppkafka .cpp file
#if (__cpluplus >= 201703L)
int CPPKAFKA_COMPILED_STL;
#else
int CPPKAFKA_COMPILED_BOOST;
#endif

Now if the cppkafka library was built with a c++11 compiler and the app with c++17, the app won't link as CPPKAFKA_COMPILED_STL is not defined. Same technique can be used to tag ABI incompatible versions.

mfontanini commented 5 years ago

If you build the library on C++14 and use boost, then you use C++17 on your application and you're using boost then you want this to build as it can do that just fine. I wouldn't expect my code to fail to build if I build in C++17 when the library was built using C++14.

accelerated commented 5 years ago

The code above was an example...instead of using #defines on the compiler version it can simply be a #define USE_BOOST which in turn could be set in the cmake file. Or a combination of both. Point being that past c++17, it's hard to justify the added dependency on boost.

marcoippolito commented 5 years ago

Could it be "fixed" adding these options in the cmake file within the next release?

sekarpdkt commented 5 years ago

Any luck with this. I would like to keep it without boost.