Using templates to build a type-safe interface to existing code

  • Thread starter Michael Le Barbier Grünewald
  • Start date
M

Michael Le Barbier Grünewald

Dear Group,

I would like to write a new, type-safe, interface to existing code. I wonder if it is possible to define a template and specialisation of this template such that use of the non-specialised template triggers an error at compile time (good) or link time (worse).

I figured out I need this to solve my interface problem, it may worth to describe it in greater details, maybe you habe an even better solution!


Existing-code is a printing facility, built around an enum, like

enum STYLE { DOUBLE1, DOUBLE2, STRING1, STRING2 }

and overloaded functions

void print(STYLE, double);
void print(STYLE, const& string);

The first parameter of the print function is used to convey styling information and, as the name suggests, DOUBLE1 and DOUBLE2 are only soundly used in conjunction with a double while STRING1 and STRING2 should be used with strings (std::string).

Thank you for your insight!
Michael
 
M

Michael Le Barbier Grünewald

Hallo Paavo,

thank you for your two nice answers! Of course your proposition to split the enum in several cases is very sound and it probably should be the way this piece of code had been written… Quite unfortunately it is not but I find static assertions very appealing to solve this!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top