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

Discussion in 'C++' started by Michael Le Barbier Grünewald, Jul 10, 2012.

  1. 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
    Michael Le Barbier Grünewald, Jul 10, 2012
    #1
    1. Advertising

  2. 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!
    Michael Le Barbier Grünewald, Jul 11, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nrm
    Replies:
    3
    Views:
    514
  2. JKop
    Replies:
    3
    Views:
    447
  3. recover
    Replies:
    2
    Views:
    781
    recover
    Jul 25, 2006
  4. ton
    Replies:
    1
    Views:
    132
  5. Replies:
    0
    Views:
    172
Loading...

Share This Page