Re: char [0-9] to int [0-9]
* Tom1s:
Cy Edmunds posted:
If you ever have a job interview and get asked this, don't use a macro
where an ordinary function will do. The interviewer might be me and
I'd show you the door in one second flat.
At which point, I, the programmer, would propose that you allocate an array
whose length is determined by an ASCII digit character:
int array[ DigitToInt( '7' ) ];
ERROR: DigitToInt( '7' ) is not a compile-time constant.
Thus, the solution:
#define DigitToInt(...
If you don't like it, then either:
a) Change language
b) Learn to live with it
c) Goad the committee to change the Standard
Note that this thread has been cross-posted to [comp.lang.c] and
[comp.lang.c++] -- generally a trolling device.
In C++ it's no big deal to define a non-macro conversion that yields a
compile time constant:
template< char digit >
struct IntFromDigit { enum{ value = digit - '\0' }; };
...
int array[IntFromDigit<'7'>::value];
If that conversion were ever required, it would be in template code, so
a template solution is very fitting.
However, normally you'd just do
std::vector<int> array(intFromDigit('7'));
with 'intFromDigit' a normal function, or, better yet,
std::vector<int> array(7);
In C 99 you do not need a compile time constant to declare the array,
and so also in C 99 you can avoid a macro.
Anyway, please use ALL UPPERCASE names for macros (and for macros only),
to avoid macros changing the meaning of non-macro code.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?