ctype&变换中的cctype [英] ctype & cctype in transform

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

问题描述

大家好,


http:// bytes.com/forum/thread60652.html 暗示在转换中使用

strtoupper不起作用,因为ctype.h可能将

strtoupper定义为宏:


"问题是标准C< ctype.h>

标头的大多数实现都将toupper / tolower / etc等函数定义为宏。要使它成为STL算法中的
,你必须包含< cctypeheader而不是

< ctype.h>。至少在我的电脑上(Debian / gcc 3.3),< cctypeundefines all

tolower / etc宏并将:: tolower / :: toupper / etc函数拉入

std命名空间,以便你的样本可以正常工作。


然而,我很确定转换调用失败的原因是

因为自动类型扣除失败。评论是否不正确?


此外,是否允许在C ++

实现中使用宏实现?


你能否将宏作为模板参数传递?例如

transform(i.begin(),alsond(),i.begin(),MY_MACRO) - 也许这会是

创建一个匿名函数?


谢谢


Taras

Hi all,

A poster at http://bytes.com/forum/thread60652.html implies that using
strtoupper in transform doesn''t work because ctype.h may define
strtoupper as a macro:

"The problem is that most implementations of the standard C <ctype.h>
header define functions like toupper/tolower/etc as macros. To make it
work in STL algorithms, you have to include <cctypeheader instead of
<ctype.h>. At least on my PC (Debian/gcc 3.3), <cctypeundefines all
tolower/etc macros and pulls ::tolower/::toupper/etc functions into
std namespace, so that your sample will work fine."

However, I''m quite sure the reason the call to transform fails is
because automatic type deduction fails. Is the comment made incorrect?

Also, are macro implementations allowed to be used in the C++
implementation?

Can you pass a macro in as a template parameter? eg
transform(i.begin(),i.end(),i.begin(), MY_MACRO) - maybe this would
create an anonymous function?

Thanks

Taras

推荐答案

2008-04- 24 10:15:36 -0400,Taras_96< ta ****** @ gmail.comsaid:
On 2008-04-24 10:15:36 -0400, Taras_96 <ta******@gmail.comsaid:

>

http://bytes.com/forum/thread60652.html 上的海报暗示使用 strtoupper不起作用,因为ctype.h可以定义

strtoupper作为宏:


问题是大多数标准C< ctype.h>

标头的实现都将toupper / tolower / etc等函数定义为宏。要使它成为STL算法中的
,你必须包含< cctypeheader而不是

< ctype.h>。至少在我的电脑上(Debian / gcc 3.3),< cctypeundefines all

tolower / etc宏并将:: tolower / :: toupper / etc函数拉入

std命名空间,以便你的样本可以正常工作。


然而,我很确定转换调用失败的原因是

因为自动类型扣除失败。评论是否不正确?


此外,是否允许在C ++

实现中使用宏实现?


你能否将宏作为模板参数传递?例如

transform(i.begin(),alsond(),i.begin(),MY_MACRO) - 也许这会是

创建一个匿名函数?
>
A poster at http://bytes.com/forum/thread60652.html implies that using
strtoupper in transform doesn''t work because ctype.h may define
strtoupper as a macro:

"The problem is that most implementations of the standard C <ctype.h>
header define functions like toupper/tolower/etc as macros. To make it
work in STL algorithms, you have to include <cctypeheader instead of
<ctype.h>. At least on my PC (Debian/gcc 3.3), <cctypeundefines all
tolower/etc macros and pulls ::tolower/::toupper/etc functions into
std namespace, so that your sample will work fine."

However, I''m quite sure the reason the call to transform fails is
because automatic type deduction fails. Is the comment made incorrect?

Also, are macro implementations allowed to be used in the C++
implementation?

Can you pass a macro in as a template parameter? eg
transform(i.begin(),i.end(),i.begin(), MY_MACRO) - maybe this would
create an anonymous function?



如果MY_MACRO是一个类似于对象的宏,该行将通过MY_MACRO扩展为的
MY_MACRO。在来自ctype.h的toupper的情况下,如果toupper是一个宏,它是一个类似函数的宏,所以它需要一个参数。当

没有参数时,宏没有被使用,名称toupper

指的是基础函数。


使用toupper的实际问题是使用

< cctype>来解决的问题是,toupper期望非负值(除非值为

是EOF),但标准并不要求char是无符号的,所以

一些有效的字符值可能是负数。


-

Pete

Roundhouse Consulting,Ltd。( www .versatilecoding.com

标准C ++库扩展:一个教程和参考的作者
www.petebecker.com/tr1book

If MY_MACRO is an object-like macro, that line would pass whatever
MY_MACRO expands to. In the case of toupper from ctype.h, if toupper is
a macro, it''s a function-like macro, so it needs an argument. When
there are no arguments the macro isn''t used, and the name "toupper"
refers to the underlying function.

The actual problem with using toupper, which isn''t solved by using
<cctype>, is that toupper expects non-negative values (unless the value
is EOF), but the standard doesn''t require that char be unsigned, so
some valid character values can be negative.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


开2008-04-24 10:33:48 -0400,Michael DOUBEZ< mi ************ @ free.frsaid:
On 2008-04-24 10:33:48 -0400, Michael DOUBEZ <mi************@free.frsaid:

>>
你能否将宏作为模板参数传递?例如
transform(i.begin(),iend(),i.begin(),MY_MACRO) - 也许这会创建一个匿名函数?
>>
Can you pass a macro in as a template parameter? eg
transform(i.begin(),i.end(),i.begin(), MY_MACRO) - maybe this would
create an anonymous function?



编号MY_MACRO将会扩展。


如果是strtoupper:

#定义strtoupper(x)((x>''''&& x<''z'')?x +(''A'' - '''''):x)


行:

transform(i.begin(),alsond(),i.begin(),strtoupper);

将是由预处理器生成的:

transform(i.begin(),alsond(),i.begin(),

((x>'' a''&& x<''z'')?x +(''A'' - ''a''):x)

);

除非你能把它变成lambda表达式,否则编译器不会理解那个


No. MY_MACRO would be expanded.

In the case of strtoupper:
#define strtoupper(x) ((x>''a'' && x<''z'')?x+(''A''-''a''):x)

The line:
transform(i.begin(),i.end(),i.begin(), strtoupper);
would be generated by the pre-processor as:
transform(i.begin(),i.end(),i.begin(),
((x>''a'' && x<''z'')?x+(''A''-''a''):x)
);

Unless you can make this a lambda expression, the compiler doesn''t
understand that :).



嗯,是的,如果电话是


transform(i.begin(),alsond(), i.begin(),toupper(x));


但是


转换(i.begin(),iend() ,i.begin(),toupper);


在语法上是可以的,因为toupper(没有任何括号)

没有命名函数式宏,但命名底层的

函数。问题是如果输入序列中除了EOF之外的值

可能是负的,那么它的运行时语义是错误的。


-

Pete

Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者

标准C ++库扩展:教程和参考
www.petebecker.com/tr1book )

Well, yes, if the call is

transform(i.begin(), i.end(), i.begin(), toupper(x));

But

transform(i.begin(), i.end(), i.begin(), toupper);

is okay syntactically, because toupper (without any parentheses)
doesn''t name the function-like macro, but names the underlying
function. The problem is that its runtime semantics are wrong if values
other than EOF in the input sequence might be negative.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)


4月24日,10:33下午,Michael DOUBEZ< michael.dou ... @ free.frwrote:
On Apr 24, 10:33 pm, Michael DOUBEZ <michael.dou...@free.frwrote:

Taras_96aécrit:
Taras_96 a écrit :

大家好,
Hi all,


海报:http://bytes.com/forum/thread60652.htmlimplies使用
$ b $变换中的strtoupper不起作用,因为ctype.h可以定义

strtoupper作为宏。
A poster athttp://bytes.com/forum/thread60652.htmlimplies that using
strtoupper in transform doesn''t work because ctype.h may define
strtoupper as a macro.



是。


Yes.


"问题在于标准C< ctype的大多数实现。 h>

标头将toupper / tolower / etc等函数定义为宏。要使它成为STL算法中的
,你必须包含< cctypeheader而不是

< ctype.h>。至少在我的电脑上(Debian / gcc 3.3),< cctypeundefines all

tolower / etc宏并将:: tolower / :: toupper / etc函数拉入

std名称空间,以便您的样本可以正常工作。
"The problem is that most implementations of the standard C <ctype.h>
header define functions like toupper/tolower/etc as macros. To make it
work in STL algorithms, you have to include <cctypeheader instead of
<ctype.h>. At least on my PC (Debian/gcc 3.3), <cctypeundefines all
tolower/etc macros and pulls ::tolower/::toupper/etc functions into
std namespace, so that your sample will work fine."





Yes


但是,我很确定调用转换失败的原因是

因为自动类型扣除失败。评论不正确吗?
However, I''m quite sure the reason the call to transform fails is
because automatic type deduction fails. Is the comment made incorrect?



你错了。预处理阶段在编译之前发生,因此

宏名称绝不是类型推导的候选者。


You are wrong. The preprocessing phase happens before compilation so the
macro name is never a candidate for type deduction.



忽略toupper可能实际上是一个宏的事实,我是什么
表明变换调用会失败因为自动

类型扣除会失败(因为toupper被重载并模板化)。

这是在 http://lists.debian.org/debian-gcc/2.../msg00092.html

如果在C ++中允许使用C标准函数*的宏定义(参见下面我的评论,请参阅

),看来你可能有*额外*问题

toupper是一个宏,所以使用toupper作为函数的输入,

期望函数指针可能会导致问题。


C ++标准是否指定

是否允许在
- < cnametype libraries ?


海报暗示宏定义在< name.h>

库中允许使用它,但不能在< cnamelibraries中使用。

Ignoring the fact that toupper might actually be a macro, what I was
suggesting that the call to transform would fail because automatic
type deduction would fail (as toupper is overloaded AND templated).
This is stated at http://lists.debian.org/debian-gcc/2.../msg00092.html.
If macro definitions of C standard functions *are* allowed in C++ (see
my comment below), it seems that you may have the *extra* problem of
toupper being a macro, so using toupper as an input to a function that
expects a function pointer may cause problems.

Does the C++ standard specify whether the standard functions are
allowed to be macro''ed in
- <name.htype libraries?
- <cnametype libraries?

The poster suggests that macro definitions are allowed in <name.h>
libraries, but not in <cnamelibraries.


>
>

此外,是否允许在C ++

实现中使用宏实现?
Also, are macro implementations allowed to be used in the C++
implementation?



如果是你的意思,C ++中允许使用宏。


Macro are allowed in C++ if it is what you mean.



我来自哪里是这句话:


我不确定符合C ++的实现是否可以有宏版本

的ctype.h头文件。我见过的大多数版本都有#ifdef __cpp__

或类似版本,使用C ++版本的内联函数和

C的宏。 - http://bytes.com/forum/thread60652.html (当然这是

语句不一定正确)。如果它*是*正确的那么

toupper的宏扩展应该永远不会出现在符合标准的

实现中,因此你不必考虑问题

toupper实际上是一个宏,当它作为一个

函数指针传递给函数时(对于标准的符合实现)


谢谢


Taras

Where I was coming from was this statement:

"I''m not sure a conforming C++ implementation can have macro versions
of the ctype.h headers. Most versions I have seen have #ifdef __cpp__
or similar, using inline functions for the C++ version and macros for
the C one." - http://bytes.com/forum/thread60652.html (of course this
statement is not necessarily correct). If it *was* correct then the
macro expansion of toupper should never occur in a conforming standard
implementation, and thus you wouldn''t have to consider the problem of
toupper actually being a macro when passing it into functions as a
function pointer (for a standard conforming implementation)

Thanks

Taras


这篇关于ctype&amp;变换中的cctype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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