编写transform_until模板 [英] Write a transform_until template

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

问题描述



我正在尝试编写transform_until模板。除了UnaryOperation返回NULL之外,它基本上正在做变换正在做的事情,它将从循环中突破



这是代码,但它有编译错误,你能告诉我是什么

我做错了吗?


模板< class _InputIter,class _OutputIter ,类_UnaryOperation>

_OutputIter

transform_until(_InputIter __first,_InputIter __last,_OutputIter

__res,

_UnaryOperation __unary_op )

{

for(;!(__ first == __last); ++ __ res,++ __ first){

//此行不编译:

_OutputIter :: value_type obj = __unary_op(* __ first);


if(obj!= NULL){

* __ res = obj;

}否则{

返回__res;

}


}

返回__res;

}


这是编译错误:

... /Utils.h:在函数''_OutputIter transform_until(_InputIter,

_InputIter,_OutputIter,_UnaryOperation)'':

... / Utils.h:88:错误:预期`;''在''obj'''
$ b之前$ b ... / Utils.h:90:错误:''obj''未在此范围内声明

解决方案

si *************** @ gmail.com 写道:

[..]
//这行不编译:
_OutputIter :: value_type obj = __unary_op(* __ first);


typename _OutputIter :: ...


并且不要使用以下划线和大写字母开头的名字,

它们由实现保留。

[..]




V

-

请在邮寄回复时从我的地址删除资金




Victor Bazarov写道:

si ************** *@gmail.com 写道:

> [..]
//这行不编译:
_OutputIter :: value_type obj = __unary_op(* __ first);



typename _OutputIter :: .. 。

并且不要使用以下划线和大写字母开头的名称,
它们由实现保留。




在任何位置包含两个相邻_ _的名称。


问候,

Michiel Salters


si ************ ***@gmail.com 写道:

这是代码,但它有编译错误,请你告诉我是什么
我做错了什么?


很多东西......

模板< class _InputIter,class _OutputIter,class _UnaryOperation>
_OutputIter
transform_until(_InputIter __first,_InputIter __last,_OutputIter
__ is,
_UnaryOperation __unary_op)


首先,所有以下划线开头的名字后跟

a大写字母或包含两个相邻下划线的

保留用于在所有上下文中实现。也就是说,上面使用的* all *

是保留的,不得使用!我猜你刚刚从一个标准的

库的标题中复制了这个样式。但请注意,这些标题不是用于教学和复制代码的b $ b b甚至可能被认为是侵犯版权的



{

for(;(__ first == __last); ++ __ res,++ __ first){


比较两个迭代器的返回类型是否相等

可兑换成''bool''但不要求''bool''。因此,

上述条件应该写成


!bool(第一个==最后一个)


这个即使在标准库实现中,错误也很常见,但是,
。另一方面,我不认为任何标准的

库实现者真的愿意修复它...

//这行不编译:
_OutputIter :: value_type obj = __unary_op(* __ first);




''value_type''是一个从属名称,因此需要资格认证
使用''typename''关键字:


typename OutputIterator :: value_type obj = unary_op(* first);


但是,迭代器不需要提供任何嵌套的typedef

,因为这是例如使用指针时无法实现。因此,类模板''iterator_traits''用于推断与迭代器相关的类型

。另外,真正的输出迭代器

将它们的值类型定义为void,从而导致另一个问题。因此,

以上行实际上应该读取


typename std :: iterator_traits< InputIterator> :: value_type obj

= unary_op (*第一个);


(当然,假设我没有在上面的

语句中引入类型)。

-

< mailto:di *********** @ yahoo.com> < http://www.dietmar-kuehl.de/>

< http://www.eai-systems.com> - 高效的人工智能


Hi,
I am trying to write a transform_until template. It is bascially doing
what transform is doing except if UnaryOperation return NULL, it will
break out from the loop.

Here is the code, but it has compile error, can you please tell me what
did I do wrong?

template <class _InputIter, class _OutputIter, class _UnaryOperation>
_OutputIter
transform_until (_InputIter __first, _InputIter __last, _OutputIter
__res,
_UnaryOperation __unary_op)
{
for (; !(__first == __last); ++__res, ++__first) {
// this line does not compile:
_OutputIter::value_type obj = __unary_op (*__first);

if (obj != NULL) {
*__res = obj;
} else {
return __res;
}

}
return __res;
}

Here is the compile error:
.../Utils.h: In function ''_OutputIter transform_until(_InputIter,
_InputIter, _OutputIter, _UnaryOperation)'':
.../Utils.h:88: error: expected `;'' before ''obj''
.../Utils.h:90: error: ''obj'' was not declared in this scope

解决方案

si***************@gmail.com wrote:

[..]
// this line does not compile:
_OutputIter::value_type obj = __unary_op (*__first);
typename _OutputIter:: ...

And don''t use names that begin with an underscore and a capital letter,
they are reserved by the implementation.
[..]



V
--
Please remove capital As from my address when replying by mail



Victor Bazarov wrote:

si***************@gmail.com wrote:

> [..]
// this line does not compile:
_OutputIter::value_type obj = __unary_op (*__first);



typename _OutputIter:: ...

And don''t use names that begin with an underscore and a capital letter,
they are reserved by the implementation.



As are names containing two adjacent _ _ at any position.

Regards,
Michiel Salters


si***************@gmail.com wrote:

Here is the code, but it has compile error, can you please tell me what
did I do wrong?
Lots of things...
template <class _InputIter, class _OutputIter, class _UnaryOperation>
_OutputIter
transform_until (_InputIter __first, _InputIter __last, _OutputIter
__res,
_UnaryOperation __unary_op)
First of all, all names starting with an underscore followed by
a capital letter or containing two adjacent underscores are
reserved for the implementation in all contexts. That is, *all*
of the names used above are reserved and shall not be used! I''d
guess you have just copied this style from one of the standard
library''s headers. Note, however, that these headers are not
intended for teaching and copying code from there might even be
considered a copyright violation.
{
for (; !(__first == __last); ++__res, ++__first) {
The return type of comparing two iterators for equality has to
be convertible to ''bool'' but is not required to be ''bool''. Thus,
the above condition should be written as

!bool(first == last)

This error is quite common even in standard library implementations,
however. On the other hand, I don''t think that any of the standard
library implementers is really willing to fix it...
// this line does not compile:
_OutputIter::value_type obj = __unary_op (*__first);



The ''value_type'' is a dependent name and thus require qualification
with the ''typename'' keyword:

typename OutputIterator::value_type obj = unary_op(*first);

However, iterators are not required to provide any nested typedefs
as this is e.g. impossible to achieve when using pointers. Thus,
the class template ''iterator_traits'' is used to infer types
associated with iterators. In addition, true output iterators
define their value type as ''void'' causing yet another problem. Thus,
the above line shall actually read

typename std::iterator_traits<InputIterator>::value_type obj
= unary_op(*first);

(assuming, of course, that I have not introduced a type into the
above statement).
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence


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

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