非const函数返回值:gcc bug还是语言缺陷? [英] non-const function return values: gcc bug or language flaw?

查看:50
本文介绍了非const函数返回值:gcc bug还是语言缺陷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你有一个按值返回值的函数,gcc编译器

(带有MinGW的Windows XP版本3.2.3)将返回的值转换为

您在代码中指定的类型,该类型的const版本。


这是一个特定于gcc的错误,还是语言中的缺陷
$ gcc努力实现的b $ b规范?例如,下面的

程序产生输出


常数

可变

常数

常数


而不是预期的


常数

可变

常数

可变


微软Visual C ++版本6给出了第二个版本(我会认为是
认为是正确一个)

--------------------------------------- ---------------------------------

#include< iostream>


class mytype

{

public:

mytype(){}

};


void print(const mytype& ival)

{

std :: cout<< "恒" << std :: endl;

}


无效打印(mytype& ival)

{

std :: cout<< "易变" << std :: endl;

}


const mytype make_const()

{

返回mytype ();

}


mytype make_mutable()

{

return mytype();

}

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

int main(){


const mytype a;

打印(a);


mytype b;

print(b);


print(make_const()); //正确打印常量


print(make_mutable()); // gcc无法打印Mutable


返回0;

}

If you have a function that returns something by value, the gcc compiler
(version 3.2.3 on Windows XP with MinGW) converts the returned value
from the type you specify in the code, to the const version of that type.

Is this a bug that is specific to gcc, or is it a flaw in the language
specification that gcc diligently implements? For example, the below
program produces the output

Constant
Mutable
Constant
Constant

instead of the expected

Constant
Mutable
Constant
Mutable

Microsofts Visual C++ version 6 gives the second version (which I would
consider to be the correct one)
------------------------------------------------------------------------
#include <iostream>

class mytype
{
public:
mytype() {}
};

void print(const mytype& ival)
{
std::cout << "Constant" << std::endl;
}

void print(mytype& ival)
{
std::cout << "Mutable" << std::endl;
}

const mytype make_const()
{
return mytype();
}

mytype make_mutable()
{
return mytype();
}
//================================================== ==
int main() {

const mytype a;
print(a);

mytype b;
print(b);

print(make_const()); // Correctly prints "Constant"

print(make_mutable()); // gcc fails to print "Mutable"

return 0;
}

推荐答案

Christian Engstr?m写道:
Christian Engstr?m wrote:

void print(const mytype& ival)
{
std :: cout< < "恒" << std :: endl;
}

void print(mytype& ival)
{
std :: cout<< "易变" << std :: endl;
}

mytype make_mutable()
{
返回mytype();
}
int main(){

print(make_mutable()); // gcc无法打印Mutable

返回0;
}

void print(const mytype& ival)
{
std::cout << "Constant" << std::endl;
}

void print(mytype& ival)
{
std::cout << "Mutable" << std::endl;
}

mytype make_mutable()
{
return mytype();
}

int main() {

print(make_mutable()); // gcc fails to print "Mutable"

return 0;
}




编译器正在做正确的事情。您对make_mutable()的调用会暂时返回

。你不能将临时函数传递给一个带有

非const引用的函数。因此,重载规则规定最后一次调用print的最佳匹配

是采用const引用的版本。

参数从非const转换为const。


-

Russell Hanneken
rg ******** @ pobox.com

删除''g' '从我的地址给我发邮件。




The compiler is doing the right thing. Your call to make_mutable() returns
a temporary. You cannot pass a temporary to a function that takes a
non-const reference. So the overloading rules dictate that the best match
for the last call to print is the version that takes a const reference. The
argument gets converted from non-const to const.

--
Russell Hanneken
rg********@pobox.com
Remove the ''g'' from my address to send me mail.



Christian Engstr?m写道:
Christian Engstr?m wrote:
如果你有一个按值返回值的函数,那么gcc
编译器(Windows XP上带有MinGW的版本3.2.3)将
返回值从你在代码中指定的类型转换为const <这种类型的版本。

这是一个特定于gcc的错误,还是gcc努力实现的语言规范中的一个缺陷?


这些都不是。这里的问题是返回值是临时的,而且C ++不允许非const引用绑定到
临时值。所以常数必须调用过载。

例如,下面的
程序产生输出

常量
可变
常量
常量

而不是预期的

常量
常量
常量

微软Visual C ++版本6给出第二个版本(我认为是正确的版本)
If you have a function that returns something by value, the gcc
compiler (version 3.2.3 on Windows XP with MinGW) converts the
returned value from the type you specify in the code, to the const
version of that type.

Is this a bug that is specific to gcc, or is it a flaw in the language
specification that gcc diligently implements?
Neither of those. The problem here is that the return value is a
temporary, and C++ doesn''t allow non-const references to be bound to
temporaries. So the "Constant" overload must be called.
For example, the below
program produces the output

Constant
Mutable
Constant
Constant

instead of the expected

Constant
Mutable
Constant
Mutable

Microsofts Visual C++ version 6 gives the second version (which I
would consider to be the correct one)




该编译器中存在允许绑定非const的错误

引用临时工。这就是它打印第二个

版本的原因。



There is a bug in that compiler that permits binding non-const
references to temporaries. That''s the reason why it prints the second
version.




" Christian Engstr?m < CH **************** @ glindra.org>在消息中写道

news:Zh ************** @ news.ecrc.de ...

"Christian Engstr?m" <ch****************@glindra.org> wrote in message
news:Zh**************@news.ecrc.de...
如果你有一个功能gcc编译器(带有MinGW的Windows XP版本3.2.3)将返回值从您在代码中指定的类型转换为该类型的const版本。 br />
这是一个特定于gcc的bug,还是gcc努力实现的语言规范中的缺陷?例如,以下
程序产生的输出

常量
常量


而不是预期的

常量
可变
常量

微软Visual C ++版本6给出了第二个版本(我会考虑到是正确的)


.NET编译器也提供了第二个版本。您是否参考了

作为评估正确性标准的标准?


------------- -------------------------------------------------- ---------
#include< iostream>

class mytype
{
公开:
mytype(){}
};

void print(const mytype& ival)
{
std :: cout<< "恒" << std :: endl;
}

void print(mytype& ival)
{
std :: cout<< "易变" << std :: endl;
}

const mytype make_const()
{
返回mytype();
}

mytype make_mutable()
{
返回mytype();
}

// ================= ================================= ==
int main(){

const mytype a;
print(a);

mytype b;
print(b);

print(make_const()); //正确打印常量

print(make_mutable()); // gcc无法打印Mutable

返回0;
}
If you have a function that returns something by value, the gcc compiler
(version 3.2.3 on Windows XP with MinGW) converts the returned value
from the type you specify in the code, to the const version of that type.

Is this a bug that is specific to gcc, or is it a flaw in the language
specification that gcc diligently implements? For example, the below
program produces the output

Constant
Mutable
Constant
Constant

instead of the expected

Constant
Mutable
Constant
Mutable

Microsofts Visual C++ version 6 gives the second version (which I would
consider to be the correct one)
The .NET compiler also gives the second version. Do you have a reference to
the standard on which to base your assessment of correctness?


------------------------------------------------------------------------
#include <iostream>

class mytype
{
public:
mytype() {}
};

void print(const mytype& ival)
{
std::cout << "Constant" << std::endl;
}

void print(mytype& ival)
{
std::cout << "Mutable" << std::endl;
}

const mytype make_const()
{
return mytype();
}

mytype make_mutable()
{
return mytype();
}
//================================================== ==
int main() {

const mytype a;
print(a);

mytype b;
print(b);

print(make_const()); // Correctly prints "Constant"

print(make_mutable()); // gcc fails to print "Mutable"

return 0;
}




-

Cy
http://home.rochester.rr.com/ cyhome /


这篇关于非const函数返回值:gcc bug还是语言缺陷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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