Q:

Different different version of same code

I developed a small soft, that just uploads files to one provider. The provider can be set before compilation.
Actually I have three same projects (with same code) but with three different provider. To illustrate, I have copied two times the folder project and modified only the source code relative to the provider.
I search a solution to join to the same project and just set a "variable" to choose the provider at the compilation
I thought to create a #define PROVIDER, with provider = [ S3 | YOUTUBE | FTP ], and then to write in code
#ifdef S3
provider = new S3Provider();
#elif YOUTUBE
provider = new YoutubeProvider();
#else
provider = new FtpProvider();
#endif

This solution is a bit ugly and I don't know other ways
Can you help me please ?
Thank you !

A:

If you want conditional compilation then preprocessor defines are the way to do it, although you can pass the values in on the command line instead of writing it into the source.
#if defined(USE_S3)
provider = new S3Provider();
#elif defined(USE_YOUTUBE)
provider = new YoutubeProvider();
#elif defined(USE_FTP)
provider = new FtpProvider();
#else
#error You must specify a provider!
#endif

And compile with
c++ -DUSE_S3 blah.cc

Note that you can't use -DPROVIDER=S3 because #if does not allow string comparison.

An alternative solution is to name the provider class on the command line:
#ifndef PROVIDER
#error You must specify a provider!
#endif

provider = new PROVIDER ();  // The space before () is important!

and then compile with
c++ -DPROVIDER=S3Provider

I prefer the first one because it better documents that three providers are implemented.

