Search code examples
c++code-duplication

C++ symbol mangling and exporting => Allow code duplication?


In our project we have something like this:

struct PointI
{
  // methods for getting, setting and calculating some point stuff
private:
  int x;
  int y;
};

struct PointD
{
  // methods for getting, setting and calculating some point stuff
private:
  double x;
  double y;
};

I proposed to change that into something like that:

template<typename T>
struct Point
{
  // methods for gettig, setting and calculating some point stuff
private:
  T x;
  T y;
};

typedef Point<int> PointI;
typedef Point<double> PointD;
typedef Point<float> PointF;

But that was refused and I was told: "There is one problem with this approach - C++ symbol mangling and exporting. Templates are so long when used in exported symbols (API that uses them) and there is no way how to export templates."

Is that argument so strong to allow lot of code duplication?


Solution

  • Your boss (or whatever) is probably right. If you write a library that should be usable from other languages than C++, it is generally a good idea to write the interface in C only.

    Of course, you can still internally use templates, just don't expose them.