帮助类型兼容性? [英] help on type compatibility?

查看:70
本文介绍了帮助类型兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好NG,


我的Lint和我的编译器不同意这是否是有效的代码:


typedef int test_t(char *) ;

typedef int contest_t(const char *);

extern contest_t somefunction;

test_t * mypointer = somefunction;


我认为作业很干净,但谁在乎呢? - 标准的结论是什么?
? (C90和C99的分辨率都非常高。

赞赏。)


谢谢,

- 方舟

Hello NG,

My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

I think the assignment is clean in what it does, but who cares? - What''s
the verdict of the standard? (Both C90 and C99 resolutions are greatly
appreciated.)

Thank you,
- Ark

推荐答案



Ark写道:

Ark wrote:
Hello NG,

我的Lint和我的编译器不同意这是否是有效的代码:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t * mypointer = somefunction;

我认为作业很干净,但谁在乎呢? - 标准的判决是什么? (C90和C99分辨率都非常值得赞赏。)
Hello NG,

My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

I think the assignment is clean in what it does, but who cares? - What''s
the verdict of the standard? (Both C90 and C99 resolutions are greatly
appreciated.)




3.5.4.3条款中的一个条款的一部分:

[1]对于两种兼容的功能类型,

应指定兼容的返回类型。此外,

参数类型列表,如果两者都存在,则

同意参数数量并使用

省略号终结符;相应的参数

应具有兼容的类型。


所以我们需要确定它们的参数是否具有兼容的类型:


''char *与const char *兼容吗?''


3.5.4.1来自其中一个条款,状态:


"对于两个兼容的指针类型,两个
应具有相同的资格,并且两者都必须

成为兼容类型的指针。


所以他们的兼容性取决于

他们是否b $ b $ 1)指向兼容类型

2)分享相同的资格


2不在此处,因此

类型不兼容。暗示

然后[1]不成立。


-

aegis


aegis写道:
Ark写道:
Ark wrote:
Hello NG,

我的Lint和我的编译器不同意这是否是有效的代码:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t * mypointer = somefunction;

我认为作业很干净,但谁在乎呢? - 标准的判决是什么? (C90和C99分辨率都非常值得赞赏。)
Hello NG,

My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

I think the assignment is clean in what it does, but who cares? - What''s
the verdict of the standard? (Both C90 and C99 resolutions are greatly
appreciated.)



3.5.4.3条款中的一个条款的一部分:
[1]For two要兼容的功能类型,都应指定兼容的返回类型。此外,参数类型列表,如果两者都存在,则应在参数数量和使用省略号终止符时一致;相应的参数
应具有兼容的类型。

所以我们需要确定它们的参数是否具有兼容的类型:

''是char *与const char *兼容'?'

3.5.4.1来自其中一个子句,状态:

"要使两个指针类型兼容,两者都
应具有相同的资格,并且都应成为兼容类型的指针。

因此,它们的兼容性取决于
它们是否1)点兼容类型2)分享相同的资格

2不在这里,因此
类型不兼容。那意味着[1]不成立。

-
aegis


Part of one of the clauses of 3.5.4.3 states:
[1] "For two function types to be compatible, both
shall specify compatible return types. Moreover,
the parameter type lists, if both are present, shall
agree in the number of parameters and in use of
the ellipsis terminator; corresponding parameters
shall have compatible types."

So we need to ascertain whether or not their parameters
have compatible type:

''Is char * compatible with const char *?''

3.5.4.1 from one of its clauses, states:

"For two pointer types to be compatible, both
shall be identically qualified and both shall
be pointers to compatible types."

So their compatibility is predicated upon
whether or not they
1) point to compatible types
2) share the same qualifications

2 does not hold here, and therefore the
types are not compatible. The implication
is then that [1] does not hold.

--
aegis



谢谢,aegis。

但是......

确实char *与const char *不兼容?

char *是指向char的非限定指针; const char *是不合格的

指向const char的指针。所以这个问题减少了char是否与const char兼容
,我认为它是......毕竟,我们经常



char c;

const char cc;

char * p;

const char * cp;

。 .......

c = cc;

cp = p;

我想进一步澄清。 ..

谢谢,

方舟


Thank you, aegis.
But...
Is it indeed that char * is not compatible with const char * ?
char * is unqualified pointer to char; const char * is unqualified
pointer to const char. So the question reduces to whether char is
compatible with const char, and I think it is... After all, we routinely
write
char c;
const char cc;
char *p;
const char *cp;
..............
c = cc;
cp = p;
I beg for further clarification...
Thank you,
Ark




Ark < AK ***** @ macroexpressions.com>在消息中写道

新闻:E _ ****************************** @ comcast.com。 ..

"Ark" <ak*****@macroexpressions.com> wrote in message
news:E_******************************@comcast.com. ..
Hello NG,
我的Lint和我的编译器不同意这是否是有效的代码:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t * mypointer = somefunction;

我认为作业很干净,但谁在乎? - 标准的判决是什么? (C90和C99的分辨率都非常值得赞赏。)
Hello NG,

My Lint and my compiler disagree on whether this is valid code:

typedef int test_t(char *);
typedef int contest_t(const char *);
extern contest_t somefunction;
test_t *mypointer = somefunction;

I think the assignment is clean in what it does, but who cares? - What''s
the verdict of the standard? (Both C90 and C99 resolutions are greatly
appreciated.)




分配是合法的,因为它可以移植来转换功能指针

到另一个函数指针类型然后再返回。 (这可以避免

需要使用函数指针类型的联合,这将是毫无意义的。

笨拙。)


但是此时非常值得警告,因为如果您之后通过错误类型的指针调用该函数,则不会发出警告



和会产生不确定的行为。


问题是调用者和被调用者必须在所有

上隐含地同意函数调用实际设置的细节起来。编译器使用它自己的规则,并且允许引用

参数的类型,包括限定符,如果有原型的话。所以通过

调用错误的原型会导致调用序列不匹配。


在这种情况下,产生的未定义行为很可能是在所有实现中都做正确的事情,除非有人知道不同......


-

RSH



The assignment is legal because it''s portable to convert a function pointer
to a different function pointer type and back again. (This can avoid the
need to use unions of function pointer types, which would be pointlessly
clumsy.)

But it''s well worth a warning at this point, because no warning can be given
later if you then call the function through the pointer of the wrong type,
and that would produce undefined behaviour.

The problem is that the caller and the callee must agree implicitly on all
the details of how the function call is actually set up. The compiler makes
its own rules about that, and it''s allowed to refer to the types of the
arguments, including qualifiers, if there''s a prototype. So calling through
the wrong prototype can produce a mismatch in the calling sequence.

In this case it''s likely that the undefined behaviour produced is to do the
right thing on all implementations, unless somebody knows different...

--
RSH


这篇关于帮助类型兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆