tags:

views:

77

answers:

2

Given a function prototype, and a type definition:

int my_function(unsigned short x);
typedef unsigned short blatherskite;

Is the following situation defined by standard:

int main(int argc, char** argv) {
  int result;
  blatherskite b;

  b=3;
  result = my_function(b);
}

Do I get type coercion predictably via the function prototype?

+8  A: 

If your question is really about whether the types of the argument and the parameter match, then the answer is yes. typedef does not introduce a new type, it only creates alias for an existing one. Variable b has type unsigned int, just like the parameter, even though b is declared using typedef-name blatherskite.

Your example is not very good for demonstrating that though. All integral types are convertible to each other in C++, so (ignoring range issues) the code would have defined behavior even if blatherskite designated a different type (a new type). But it doesn't. So this is also perfectly valid

void foo(unsigned int* p);
...
blatherskite *pb = 0;
foo(pb); // <- still valid
AndreyT
That is exactly what I was wondering. I'm curious what other question you thought I might be asking.
David
Well, if you believed the types didn't match, the question might be different: is `blatherskite` convertible to `unsigned int`?
AndreyT
Ah, I see. I do believe in this case that the types match. This is maybe one of the problems. As I noted to Jerry below, I have a lot of junk names floating around.I don't know that I could construe a blatherskite as an unsigned int, but if I wanted to construe an unsigned int as a blatherskite, I could probably use the explicit keyword on a single argument constructor that takes an unsigned int.I'm curious about if this conversion would happen automatically in the case that I described above. I'll ask it as a follow up [after the meeting that I am late for].
David
+3  A: 

No type coercion is needed. The typedef is just an alias for the same type, so you're passing an unsigned short to a function that takes an unsigned short.

Jerry Coffin
Thanks Jerry. In the actual code, I have maybe as many as ten different aliases named for a particular intrinsic type. Those names have been used out of their semantic context so much, it made me wonder about the reliability. It reportedly 'works' but the definition of 'works' is very fuzzy. ['Works' may mean as little as, "Hey, it compiled."]
David
@David: yup -- there has been talk of adding a "strong typedef" (that would create a new type, not just an alias) but it hasn't happened yet (and may never happen -- who knows).
Jerry Coffin