typedef麻烦 [英] typedef trouble

查看:76
本文介绍了typedef麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,如果所述类型用于返回值,我无法将const限定符添加到typedefed指针类型

。如果使用类型,它确实有用

参数。

我没有看到这背后的逻辑,但我认为它符合标准

行为。我使用GCC,我猜这么明显的bug。 (如果是一个)

早就报告并修复过了。


示例:


typedef char MY_CHAR;

typedef MY_CHAR * MY_STRING;


void foo(const MY_STRING str)//确实有效


const MY_STRING bar(void)//不起作用

(GCC说const限定符被忽略)

解决方案

" copx" < co ** @ gazeta.plwrites:


由于某种原因,我无法将const限定符添加到typedefed指针类型

如果说type用于返回值。如果使用类型,它确实有用

参数。

我没有看到这背后的逻辑,但我认为它符合标准

行为。我使用GCC,我猜这么明显的bug。 (如果是一个)

早就报告并修复过了。


示例:


typedef char MY_CHAR;

typedef MY_CHAR * MY_STRING;


void foo(const MY_STRING str)//确实有效


const MY_STRING bar(void)//不起作用

(GCC说const限定符被忽略)



但它确实有效。警告只是告诉你它没用。


鉴于上面的声明,类型为const MY_STRING。并不意味着

指向const char的指针,它意味着const指向char的指针。说

函数返回的值是const并不意味着什么;

它是一个值,而不是一个对象,所以没有办法无论如何要修改它。


在参数声明中,它是有意义的,但它可能并不意味着

你认为它意味着什么。这意味着foo主体中的代码不能修改str(这对调用者来说没有意义);它仍然可以

修改str指向的内容。


顺便说一句,隐藏在typedef后面的指针类型通常不是一个好主意,并将char *作为字符串(或MY_STRING)引用

可能具有误导性。 char *不是字符串;它可能会也可能不会
指向一个字符串。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

Nokia

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


是的,copx是正确的。


以下声明是直截了当的。

" ANSI允许类型限定符为常量或不稳定,但是

应用于函数返回类型的限定符是没有意义的,因为

函数只能返回rvalues,类型限定符只适用于lvalues




上述声明澄清了您的疑问。无论GCC是什么,

抱怨只是一个''警告'',''const''说明符被忽略,

这不是错误。


但我仍然感到困惑:


为什么海湾合作委员会没有提出以下定义的警告:


const char * bar(无效)

{

}


但它确实适用于


typedef char * MY_STRING

const MY_STRING bar(无效)

{

}


有人可以澄清吗?


-Kiran


5月22日上午11:47,Keith Thompson< ks .. 。@ mib.orgwrote:


" copx" < c ... @ gazeta.plwrites:


由于某种原因,我不能将const限定符添加到typedefed指针类型

if所述类型用于返回值。如果使用类型,它确实有用

参数。

我没有看到这背后的逻辑,但我认为它符合标准

行为。我使用GCC,我猜这么明显的bug。 (如果是一个)

早就报告并修复了。


示例:


typedef char MY_CHAR;

typedef MY_CHAR * MY_STRING;


void foo(const MY_STRING str)//工作


const MY_STRING bar(void)//不起作用

(GCC说const限定符被忽略)



但它确实有效。警告只是告诉你它没用。


鉴于上面的声明,类型为const MY_STRING。并不意味着

指向const char的指针,它意味着const指向char的指针。说

函数返回的值是const并不意味着什么;

它是一个值,而不是一个对象,所以没有办法无论如何要修改它。


在参数声明中,它是有意义的,但它可能并不意味着

你认为它意味着什么。这意味着foo主体中的代码不能修改str(这对调用者来说没有意义);它仍然可以

修改str指向的内容。


顺便说一句,隐藏在typedef后面的指针类型通常不是一个好主意,并将char *作为字符串(或MY_STRING)引用

可能具有误导性。 char *不是字符串;它可能会也可能不会

指向一个字符串。


-

Keith Thompson(The_Other_Keith)ks ... @ mib.org< http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


很抱歉在我的第一份声明中将''Tompson''称为''Copx'。


Kiran

5月22日上午11:54,Kiran Kumar Mukkamala< k.mukkam ... @ gmail.com>

写道:


是的copx是正确的。


以下声明是直截了当的。

ANSI允许类型限定符是const或volatile,但是应用于函数返回类型的
限定符是没有意义的,因为

函数只能返回rvalues并且类型限定符仅适用

到左值。


上述声明澄清了您的疑问。无论GCC是什么,

抱怨只是一个''警告'',''const''说明符被忽略,

这不是错误。


但我仍然感到困惑:


为什么海湾合作委员会没有提出以下定义的警告:


const char * bar(无效)

{


}


但它适用于


typedef char * MY_STRING

const MY_STRING bar(无效)

{


}


有人可以澄清一下吗?


-Kiran


5月22日上午11:47 ,Keith Thompson< ks ... @ mib.orgwrote:


" copx" < c ... @ gazeta.plwrites:


由于某种原因,我不能将const限定符添加到typedefed指针类型

if所述类型用于返回值。如果使用类型,它确实有用

参数。

我没有看到这背后的逻辑,但我认为它符合标准

行为。我使用GCC,我猜这么明显的bug。 (如果是一个)

早就报告并修复了。


示例:


typedef char MY_CHAR;

typedef MY_CHAR * MY_STRING;


void foo(const MY_STRING str)//工作


const MY_STRING bar(void)//不起作用

(GCC说const限定符被忽略)


但它确实有效。警告只是告诉你它没用。


鉴于上述声明,类型为const MY_STRING。并不意味着

指向const char的指针,它意味着const指向char的指针。说

函数返回的值是const并不意味着什么;

它是一个值,而不是一个对象,所以没有办法无论如何要修改它。


在参数声明中,它是有意义的,但它可能并不意味着

你认为它意味着什么。这意味着foo主体中的代码不能修改str(这对调用者来说没有意义);它仍然可以

修改str指向的内容。


顺便说一句,隐藏在typedef后面的指针类型通常不是一个好主意,并且将char *称为a *字符串(或MY_STRING)

可能具有误导性。 char *不是字符串;它可能是也可能不是
指向一个字符串。


-

Keith Thompson(The_Other_Keith)ks ... @ mib.org< http:// www .ghoti.net / ~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长


For some reason I cannot add a const qualifier to a typedefed pointer type
if said type is used for a return value. It does work if the type is used
for a parameter.
I do not see the logic behind this, but I assume it is standard conforming
behaviour. I use GCC and I guess such an obvious "bug" (if it were one)
would have been reported and fixed long ago.

Example:

typedef char MY_CHAR;
typedef MY_CHAR * MY_STRING;

void foo(const MY_STRING str) // does work

const MY_STRING bar(void) // does not work
(GCC says the const qualifier is ignored)

解决方案

"copx" <co**@gazeta.plwrites:

For some reason I cannot add a const qualifier to a typedefed pointer type
if said type is used for a return value. It does work if the type is used
for a parameter.
I do not see the logic behind this, but I assume it is standard conforming
behaviour. I use GCC and I guess such an obvious "bug" (if it were one)
would have been reported and fixed long ago.

Example:

typedef char MY_CHAR;
typedef MY_CHAR * MY_STRING;

void foo(const MY_STRING str) // does work

const MY_STRING bar(void) // does not work
(GCC says the const qualifier is ignored)

But it does work. The warning merely tells you that it''s useless.

Given the declarations above, the type "const MY_STRING" doesn''t mean
"pointer to const char", it means "const pointer to char". Saying
that the value returned by a function is const doesn''t mean anything;
it''s a value, not an object, so there''s no way to modify it anyway.

In the parameter declaration, it''s meaningful, but it may not mean
what you think it means. It means that code in the body of foo can''t
modify the str (which is meaningless to the caller); it can still
modify what str points to.

Incidentally, hiding pointer types behind typedefs is usually not a
good idea, and referring to a char* as a string (or MY_STRING) is
potentially misleading. A char* is not a string; it may or may not
point to a string.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Yes copx is correct.

The following statement is straight forward.
"ANSI allows the type-qualifier to be const or volatile , but either
qualifier applied to a function return type is meaningless, because
functions can only return rvalues and the type qualifiers apply only
to lvalues."

The above statement clarifies the your doubt. Whatever GCC is
complaining is only a ''Warning'', that ''const'' specifier is ignored,
it''s not an error.

But still I am puzzled:

why GCC doesn''t raise the warning for following definition:

const char * bar(void)
{
}

but it does for

typedef char * MY_STRING
const MY_STRING bar(void)
{
}

Some one can clarify ?

-Kiran

On May 22, 11:47 am, Keith Thompson <ks...@mib.orgwrote:

"copx" <c...@gazeta.plwrites:

For some reason I cannot add a const qualifier to a typedefed pointer type
if said type is used for a return value. It does work if the type is used
for a parameter.
I do not see the logic behind this, but I assume it is standard conforming
behaviour. I use GCC and I guess such an obvious "bug" (if it were one)
would have been reported and fixed long ago.

Example:

typedef char MY_CHAR;
typedef MY_CHAR * MY_STRING;

void foo(const MY_STRING str) // does work

const MY_STRING bar(void) // does not work
(GCC says the const qualifier is ignored)


But it does work. The warning merely tells you that it''s useless.

Given the declarations above, the type "const MY_STRING" doesn''t mean
"pointer to const char", it means "const pointer to char". Saying
that the value returned by a function is const doesn''t mean anything;
it''s a value, not an object, so there''s no way to modify it anyway.

In the parameter declaration, it''s meaningful, but it may not mean
what you think it means. It means that code in the body of foo can''t
modify the str (which is meaningless to the caller); it can still
modify what str points to.

Incidentally, hiding pointer types behind typedefs is usually not a
good idea, and referring to a char* as a string (or MY_STRING) is
potentially misleading. A char* is not a string; it may or may not
point to a string.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


Sorry to refer ''Tompson'' as ''Copx''in my 1st statement.

Kiran
On May 22, 11:54 am, Kiran Kumar Mukkamala <k.mukkam...@gmail.com>
wrote:

Yes copx is correct.

The following statement is straight forward.
"ANSI allows the type-qualifier to be const or volatile , but either
qualifier applied to a function return type is meaningless, because
functions can only return rvalues and the type qualifiers apply only
to lvalues."

The above statement clarifies the your doubt. Whatever GCC is
complaining is only a ''Warning'', that ''const'' specifier is ignored,
it''s not an error.

But still I am puzzled:

why GCC doesn''t raise the warning for following definition:

const char * bar(void)
{

}

but it does for

typedef char * MY_STRING
const MY_STRING bar(void)
{

}

Some one can clarify ?

-Kiran

On May 22, 11:47 am, Keith Thompson <ks...@mib.orgwrote:

"copx" <c...@gazeta.plwrites:

For some reason I cannot add a const qualifier to a typedefed pointer type
if said type is used for a return value. It does work if the type is used
for a parameter.
I do not see the logic behind this, but I assume it is standard conforming
behaviour. I use GCC and I guess such an obvious "bug" (if it were one)
would have been reported and fixed long ago.

Example:

typedef char MY_CHAR;
typedef MY_CHAR * MY_STRING;

void foo(const MY_STRING str) // does work

const MY_STRING bar(void) // does not work
(GCC says the const qualifier is ignored)

But it does work. The warning merely tells you that it''s useless.

Given the declarations above, the type "const MY_STRING" doesn''t mean
"pointer to const char", it means "const pointer to char". Saying
that the value returned by a function is const doesn''t mean anything;
it''s a value, not an object, so there''s no way to modify it anyway.

In the parameter declaration, it''s meaningful, but it may not mean
what you think it means. It means that code in the body of foo can''t
modify the str (which is meaningless to the caller); it can still
modify what str points to.

Incidentally, hiding pointer types behind typedefs is usually not a
good idea, and referring to a char* as a string (or MY_STRING) is
potentially misleading. A char* is not a string; it may or may not
point to a string.

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于typedef麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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