I
Ian Collins
Ian said:else ?
Oops, I didn't spot the 'something like'!
Ian said:else ?
Alex Buell said:Oh yes. Don't need an else, but I'd put in a return -1 just in case
input isn't valid.
Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.
Alex said:I'd do something like this:
short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
}
The meaning of the code is simpler
if you use type int for your parameter and return type,
otherwise you have implicit conversions all over the place.
The number of standard library functions which use
either char or short, for parameter type or return type,
is zero.
Alex Buell said:Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.
short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';
return -1;
}
Keith said:No, I just misunderstood. When you said "Don't need an else", I
thought you meant leaving the code as it was originally (which falls
off the end of the function if the argument is out of range).
I'd probably put the "return -1;" in an else clause, but that's just a
matter of style.
I'd also have the function return int rather than short.
Cy said: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.
I don't think I'd bother with the macros. Just using c-'0' or d+'0'
seems clear enough to me. YMMV.
int char2digt(int c)
{
return c => '0' && c <= '9 ? c - '0' : -1;
}
Richard said:Keith Thompson said:
In fact, this is a case where the code
is actually clearer than the macro
names that purport to represent it.
Why, for example, is there a number 2
in those names?
Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.
Tomás said:Cy Edmunds posted:
At which point, I, the programmer, would propose
[snip]
Hold it right there. Once I show you the door you ain't proposing
anything!
Jordan Abel said:Tomás said: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
[snip]
Hold it right there. Once I show you the door you ain't proposing
anything!
Well, you shouldn't be showing people the door based on an incorrect
assumption, namely [that a call to an ordinary function can be used
in
all contexts where a use of a macro can].
Keith said:(I just noticed this is cross-posted to comp.lang.c++. I presume C++
has the same rules in this area as C, but I'm not certain.
Robert said:That's your justification for using a macro? You've got to be joking.
Alf said: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'));
Pete said:If anyone proposed this as a solution to the original problem, which was
to get a compile-time constant, I'd show them the door in one second
flat. Normal or otherwise, it does't solve the problem.
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.