Constness of return-by-value temporaries
It's perfectly legitimate to use "const_cast", and subsequently alter an
object, but only if the object was defined as non-const in the first place.
Example 1:
#include <string>
void Func( const std::string &cstr )
{
std::string &str = const_cast<std::string&>(cstr);
str += "monkey";
}
int main()
{
std::string str("ape");
Func(str);
}
I've seen many examples of where a temporary object is non-const. Example 2:
#include <string>
#include <vector>
std::vector<std::string> Func()
{
std::vector<std::string> vec;
vec.push_back("One");
vec.push_back("Two");
return vec;
}
int main()
{
Func().push_back("Three"); // <-- Non-const member function
}
(I'll admit that I'm not certain if ALL temporaries are non-const throughout
the entire language. Could someone please clarify that?)
We can use a const reference to "extend the lifetime" of a temporary
returned-by-value from a function. Example 3:
#include <string>
#include <vector>
std::vector<std::string> Func()
{
std::vector<std::string> vec;
vec.push_back("One");
vec.push_back("Two");
return vec;
}
int main()
{
const std::vector<std::string> &cvec = Func();
std::string str( cvec.at(0) );
str += "monkey";
}
We saw in Example 2, that the temporary object returned-by-value from
"Func" was non-const.
I have a question, which I have asked on comp.lang.c++, but which no-one has
seemed willing to answer. Is the following translation unit well-formed, and
absent of undefined behaviour?
#include <string>
using std::string;
#include <vector>
using std::vector;
vector<string> Func()
{
vector<string> vec;
vec.push_back("One");
vec.push_back("Two");
return vec;
}
#include <iostream>
using std::cout; using std::endl;
int main()
{
const vector<string> &cvec = Func();
vector<string> &vec = const_cast< vector<string>& >(cvec);
vec.push_back("Three");
cout << vec.at(0) << endl
<< vec.at(1) << endl
<< vec.at(2) << endl;
}
-Tom1s
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]