字符串文字非常量? [英] String literals non const?

查看:87
本文介绍了字符串文字非常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么字符串文字被视为char *而不是const char *?


(1)void f(char *);

(2) void f(const char *);


f(" foo")将调用函数f的版本(1)。


我明白了文字foo的确切类型是char [4],它通过标准转换的
变为char *。不过,这里出现了问题

,因为这允许修改foo,在我看来应该禁止
(因为它会导致未定义的行为) )。


祝你好运,

Marcin

解决方案

Marcin Kalicinski写道:

为什么字符串文字被视为char *而不是const char *?

(1)void f(char *);
(2)void f (const char *);

f(" foo")将调用函数f的版本(1)。

我理解文字的确切类型foo ;是char [4],它通过标准转换的方式变成char *。不过,这里出现了问题,因为这样可以修改foo,我认为这应该被禁止(因为它会导致未定义的行为)。

最好的问候,
Marcin




应该是const。第2.13.4.1节陈述

``普通字符串文字的类型为n const char的数组和

静态存储持续时间,其中n是字符串的大小,如下面定义的

,并用给定的字符初始化。''''


HTH,





所以VC ++ .NET编译器似乎有问题。以下

代码:


// ======================== ==

#include< iostream>

void main()

{

std :: cout< ;< typeid(" foo")。name()<< " \ n";

}

// ======================== ==


输出char [4]。以下是:


// ==========================

#include< iostream>

int f(char *){return 1; }

int f(const char *){return 2; }

void main()

{

std :: cout<< f(foo)<< " \ n";

}

// ======================== ==


输出1.

目前我无法访问任何其他编译器,但我不知道是否

这是正常行为吗?如果标准说它应该是常量字符[4],

如果这是char [4]则是一个严重的问题。


祝你好运,$ / b $ B $ Marcin Kalicinski写道:

为什么字符串文字被视为char *而不是const char *?

(1)void f(char *);
(2)void f(const char *);

f(" foo")将调用版本( 1)函数f。

我理解文字foo的确切类型。是char [4],通过
标准转换的方式变为char *。尽管如此,这里还有一些错误的
,因为这允许修改foo,在我看来应该禁止
(因为它会导致未定义的行为)。

最好的问候,
Marcin



它应该是常量。第2.13.4.1节声明``一个普通的字符串文字具有n const char的类型数组和
静态存储持续时间,其中n是下面定义的字符串大小,并用给定的字符初始化。''''

HTH, Jacques



Marcin Kalicinski写道:



所以似乎有VC ++ .NET编译器有问题。以下
代码:

// ==========================
#include < iostream>
void main()
{
std :: cout<< typeid(" foo")。name()<< " \ n";
}
// ==========================



有趣的是,GCC 3.2.3并没有通过typeid显示

char []和const char []之间的区别.name() -

cout<< typeid(char [4])。name()<< ''\ n''

<< typeid(const char [4])。name();

表示

A4_c

A4_c


它调用了正确的函数..它匹配void f(const char *)。


HTH,

雅克。


Why string literals are regarded as char * not as const char *?

(1) void f(char *);
(2) void f(const char *);

f("foo") will call version (1) of function f.

I understand that the exact type of literal "foo" is char[4], which by means
of standard conversion becomes char *. Still, there''s something going wrong
here, because this allows modification of "foo", which in my opinion should
be forbidden (because it causes undefined behavior).

Best regards,
Marcin

解决方案

Marcin Kalicinski wrote:

Why string literals are regarded as char * not as const char *?

(1) void f(char *);
(2) void f(const char *);

f("foo") will call version (1) of function f.

I understand that the exact type of literal "foo" is char[4], which by means
of standard conversion becomes char *. Still, there''s something going wrong
here, because this allows modification of "foo", which in my opinion should
be forbidden (because it causes undefined behavior).

Best regards,
Marcin



It should be const. Section 2.13.4.1 states
``An ordinary string literal has type "array of n const char" and a
static storage duration where n is the size of the string as
defined below, and is initialized with the given characters.''''

HTH,
Jacques


Hi,

So it seems there''s something wrong with VC++ .NET compiler. The following
code:

//==========================
#include <iostream>
void main()
{
std::cout << typeid("foo").name() << "\n";
}
//==========================

Outputs "char[4]". And the following one:

//==========================
#include <iostream>
int f(char *) { return 1; }
int f(const char *) { return 2; }
void main()
{
std::cout << f("foo") << "\n";
}
//==========================

Outputs 1.

At the moment I do not have access to any other compiler, but I wonder if
this is normal behavior? If the standard says it should be const char [4],
it''s a serious problem if this is char[4] instead.

Best regards,
Marcin

Marcin Kalicinski wrote:

Why string literals are regarded as char * not as const char *?

(1) void f(char *);
(2) void f(const char *);

f("foo") will call version (1) of function f.

I understand that the exact type of literal "foo" is char[4], which by means of standard conversion becomes char *. Still, there''s something going wrong here, because this allows modification of "foo", which in my opinion should be forbidden (because it causes undefined behavior).

Best regards,
Marcin



It should be const. Section 2.13.4.1 states
``An ordinary string literal has type "array of n const char" and a
static storage duration where n is the size of the string as
defined below, and is initialized with the given characters.''''

HTH,
Jacques



Marcin Kalicinski wrote:

Hi,

So it seems there''s something wrong with VC++ .NET compiler. The following
code:

//==========================
#include <iostream>
void main()
{
std::cout << typeid("foo").name() << "\n";
}
//==========================

Outputs "char[4]". And the following one:


Interestingly enough, GCC 3.2.3 doesn''t show the difference between
char[] and const char[] through typeid.name() -
cout << typeid(char[4]).name() << ''\n''
<< typeid(const char[4]).name();
says
A4_c
A4_c

It calls the correct function though.. it matches void f(const char*).

HTH,
Jacques.


这篇关于字符串文字非常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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