?
=?ISO-8859-1?Q?Christian_Engstr=F6m?=
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;
}
(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;
}