0

I have to use some library that for some reason has very ugly classes:

template<char chr1='a', char chr2='b',char... _T_NAME>
class CParserVarFloat: public CParserVarNamed<chr1,chr2, _T_NAME ...>{
    ...
}

Yes, they could just use std::string (especially when, inside of that class' constructor, they create temporary std::vector to store the characters...). But they did not.

In order to create the variable I would have to write:

CParserVarFloat<'t', 'e', 'x', 't', 0, [[45 times 0 here]] > myVariable;

But I would like to create some c++ macro, so the code could look like:

CParserVarFloat<CHAR50_ARR("text")> myVariable;

So basically I need to convert "text" into 't', 'e', 'x', 't', 0, [[45 times 0 here]] with a macro. Note that I cannot use dynamic arrays as the parameters must be declared in compile-time.

The library suggests to use some _CONST_CHAR(s):

#define _CONST_CHAR(s)\
getChr(s,0),\
getChr(s,1),\
getChr(s,2),\
getChr(s,3),\
getChr(s,4),\
getChr(s,5),\
getChr(s,6),\
getChr(s,7),\
getChr(s,8),\
getChr(s,9),\
getChr(s,10),\
getChr(s,11),\
getChr(s,12),\
getChr(s,13),\
getChr(s,14),\
getChr(s,15),\
getChr(s,16),\
getChr(s,17),\
getChr(s,18),\
getChr(s,19),\
getChr(s,20),\
getChr(s,21),\
getChr(s,22),\
getChr(s,23),\
getChr(s,24),\
getChr(s,25),\
getChr(s,26),\
getChr(s,27),\
getChr(s,28),\
getChr(s,29),\
getChr(s,30),\
getChr(s,31),\
getChr(s,32),\
getChr(s,33),\
getChr(s,34),\
getChr(s,35),\
getChr(s,36),\
getChr(s,37),\
getChr(s,38),\
getChr(s,39),\
getChr(s,40),\
getChr(s,41),\
getChr(s,42),\
getChr(s,43),\
getChr(s,44),\
getChr(s,45),\
getChr(s,46),\
getChr(s,47),\
getChr(s,48),\
getChr(s,49),\
getChr(s,50)

#define getChr(name, ii) ((MIN(ii,MAX_CONST_CHAR))<strlen(name)?name[ii]:0)

Which is both ugly and non-constant at all (the code does not compile on VC++ 2013, because of ... ? name[ii]:0 part).

PolGraphic
  • 3,233
  • 11
  • 51
  • 108
  • 1
    Why 45 times 0 ? No way to redesign your client API to avoid this monstrosity ? – quantdev Jan 04 '15 at 14:28
  • I do not only need the 'a', 'b', 'c' (which is more trivial), but also the 0, 0, 0 (as much as are needed to satisfy: strlen("abc") + count of zeros = 50). – PolGraphic Jan 04 '15 at 14:29
  • @PolGraphic http://coliru.stacked-crooked.com/a/ae199a4dbd08d9f2 – Piotr Skotnicki Jan 04 '15 at 14:31
  • @quantdev I will for sure re-design the library to use `std::string` or `char *`, but for now I want to get it "compilable" so I can check if it works (fast) and decide if the efforts of re-writing parts of library are worth my time. Besides that monstrosity (great name!) convention, it has some complex logic that I need. – PolGraphic Jan 04 '15 at 14:31
  • It's not a good sign if the library has such an interface and the suggested macros and workarounds don't compile. – sth Jan 04 '15 at 14:37
  • Have a template with 50 default zeros, derived from CParserVarFloat (Though, it will not give you strings) –  Jan 04 '15 at 14:46

0 Answers0