转换运算符有什么问题? [英] anything wrong with conversion operator?

查看:48
本文介绍了转换运算符有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:


#include< string>

#include< iostream>

using namespace std;


struct S

{

const char * ps_;

operator string();

};


int main()

{

s s;

cout<< s;

}


运算符<<(ostream&,const string&)在标准中定义并且

从S到字符串的转换也是。但编译器拒绝此代码

因为运算符<(<(ostream&,const S&)未定义。

这段代码出了什么问题?


-

ES Kim

Consider:

#include <string>
#include <iostream>
using namespace std;

struct S
{
const char* ps_;
operator string();
};

int main()
{
S s;
cout << s;
}

operator<<(ostream&, const string&) is defined in the standard and
the conversion from S to string is, too. But compilers reject this code
since operator<<(ostream&, const S&) is not defined.
What''s wrong with this code?

--
ES Kim

推荐答案

* ES Kim:
考虑:

#include< string>
#include< iostream>
使用命名空间std;

struct S
{
const char * ps_;
运算符字符串();
};

int main()
{s / s s s ;
cout<< s;
}

运算符<(ostream&,const string&)在标准中定义,并且从S到字符串的转换也是如此。


神圣标准没有说明你的结构S.


但编译器拒绝此代码


因为它们应该。


因为未定义运算符<(ostream&,const S&)。


如果你确定这就是原因...


这段代码出了什么问题?
Consider:

#include <string>
#include <iostream>
using namespace std;

struct S
{
const char* ps_;
operator string();
};

int main()
{
S s;
cout << s;
}

operator<<(ostream&, const string&) is defined in the standard and
the conversion from S to string is, too.
The Holy Standard says nothing about your struct S.

But compilers reject this code
As they should.

since operator<<(ostream&, const S&) is not defined.
If you''re sure that''s the reason...

What''s wrong with this code?




....那你为什么这么问?


这是家庭作业吗?


无论如何,即使代码已经更正(总是发布完整的代码,如果可能的话,也可以是b $ b)MSVC 7.1拒绝它。然而,转换

到''char const *''没有问题。我不知道为什么,但是在标准库的MSVC实现中,运算符<<<<<<<<<对于

字符串必须推断模板参数,例如

字符串的分配器类型;为什么这个问题只会令我感到困惑,因为两个运营商都必须推断出流的模板参数。

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门帖子。

问:usenet和电子邮件中最烦人的事情是什么?



.... then why are you asking this?

Is this homework?

Anyway, even with the code corrected (always post code that is complete,
if possible) MSVC 7.1 rejects it. It has no problem with conversion
to ''char const*'', however. I don''t know why, but one slight difference is
that in the MSVC implementation of the standard library the operator<< for
strings must infer template arguments such as the allocator type for the
string; why that should matter just baffles me, since both operator
implementations must infer template arguments for the stream.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


* Alf P. Steinbach:
* Alf P. Steinbach:
* ES Kim:
* ES Kim:
考虑:

#include< string>
#include< iostream>
使用命名空间std;

struct S
{const char * ps_;
运算符字符串();
};

int main()
{
S s;
cout<< s;
}
Consider:

#include <string>
#include <iostream>
using namespace std;

struct S
{
const char* ps_;
operator string();
};

int main()
{
S s;
cout << s;
}



无论如何,即使代码已经更正(总是发布完整的代码,如果可能的话),MSVC 7.1拒绝它。然而,转换为''char const *''没有问题。我不知道为什么,但一个细微的差别是,在标准库的MSVC实现中,运算符<<对于
字符串必须推断模板参数,例如
字符串的分配器类型;为什么这只会让我感到困惑,因为两个运算符实现都必须推断流的模板参数。



Anyway, even with the code corrected (always post code that is complete,
if possible) MSVC 7.1 rejects it. It has no problem with conversion
to ''char const*'', however. I don''t know why, but one slight difference is
that in the MSVC implementation of the standard library the operator<< for
strings must infer template arguments such as the allocator type for the
string; why that should matter just baffles me, since both operator
implementations must infer template arguments for the stream.




将以下内容添加到混合中(在类S之后) ,例如)工作所以它

似乎与模板参数演绎有关 - 有一个规则

关于正在尝试直接转换,但我不是'' t完全记得:


模板< class Traits>内联

basic_ostream< char,Traits>&运算符<<(

basic_ostream< char,Traits>& stream,

const basic_string< char>& s



{

返回std :: operator<<(stream,s);

}


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



Adding the following to the mix (after class S, for example) works so it
seems to have to do with template argument deduction -- there is a rule
about just direct conversions being tried, but I don''t recall exactly:

template<class Traits> inline
basic_ostream<char, Traits>& operator<<(
basic_ostream<char, Traits>& stream,
const basic_string<char>& s
)
{
return std::operator<<( stream, s );
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


" Alf P 。斯坦巴赫 <人*** @ start.no>在消息中写道

news:41 **************** @ news.individual.net ...
"Alf P. Steinbach" <al***@start.no> wrote in message
news:41****************@news.individual.net...

...代码剪切
运算符<<(ostream&,const string&)在标准中定义,并且从S到字符串的转换也是如此。
神圣标准没有说明你的结构S.

... code snipped
operator<<(ostream&, const string&) is defined in the standard and
the conversion from S to string is, too.
The Holy Standard says nothing about your struct S.

但是编译器拒绝这个代码
But compilers reject this code



正如他们应该的那样。



As they should.

因为运算符<(<(ostream&,const S&)未定义。
since operator<<(ostream&, const S&) is not defined.



如果你''确定这就是原因......



If you''re sure that''s the reason...

这段代码出了什么问题?
What''s wrong with this code?



..那你为什么要问这个?

这是家庭作业吗?



... then why are you asking this?

Is this homework?




我希望如此。我太老了,无法做到这一点。 ;-)

无论如何,即使代码已经更正(总是发布完整的代码,如果可能的话),MSVC 7.1拒绝它。然而,转换为''char const *''没有问题。我不知道为什么,


我也不是,这就是我问这个问题的原因。
好​​的,让我澄清一下。 br />

struct S

{

operator const char *();

};


s s;

cout<< s;


如果你像这样定义S,它就没问题了,运算符<<(ostream&,const char *)

被调用。为什么不使用operator string()?

,但是在标准库的MSVC实现中,运算符<<<<<对于
字符串必须推断模板参数,例如
字符串的分配器类型;为什么这只会让我感到困惑,因为两个运算符实现都必须推断流的模板参数。



I wish it were. I''m too old to do such a thing. ;-)

Anyway, even with the code corrected (always post code that is complete,
if possible) MSVC 7.1 rejects it. It has no problem with conversion
to ''char const*'', however. I don''t know why,
Neither do I, and that''s why I asked the question.
Ok, let me clarify.

struct S
{
operator const char*();
};

S s;
cout << s;

If you define S like this, it''s fine, and operator<<(ostream&, const char*)
is called. Why not with operator string()?
but one slight difference is
that in the MSVC implementation of the standard library the operator<< for
strings must infer template arguments such as the allocator type for the
string; why that should matter just baffles me, since both operator
implementations must infer template arguments for the stream.




原始代码不仅被MSVC拒绝,而且被拒绝还有gcc和comeau。

所以我认为这不仅仅是一个实现问题,而是与语言相关的问题。

-

ES Kim



The original code is rejected not just with MSVC but also with gcc and comeau.
So I think it''s not just an implementation problem but language-related one.
--
ES Kim


这篇关于转换运算符有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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