문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top