Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top