Missing const in many parts of the C++ library

Hi,

while programming device servers in C++ I found an issue using DeviceData with std::string.
The operator to store an std::string into the DeviceData object takes an argument passed by reference that is not const.

DeviceData.h:383
void operator << (string &datum) {any <<= datum.c_str();}


But the 'any' object will make a copy of the string. So why is it not passed by const reference?
This will prevent for example to write code like this:
DeviceData d;
stringstream message;
message << "some text";
d << message.str();


By studying the library code, I found other occurrences where a const specifier for string arguments may be included.

For example, when storing a string value inside a DbDatum a copy is made, so it is not needed for the argument of the operator to be modifiable. The reference can then be to a const string.

dbapi_datum.cpp:783
void DbDatum::operator << (string& datum)


In DeviceImpl definition the member function get_name() return a writable reference to the device name. Is this by design? If the name is modifiable, shouldn't be with a set_name method? Also you will not be able to read the name from a pointer to a const device, since the method is not const.
device.h:240
string &get_name() {return device_name;}


Are all these const omitted by design?
In principle adding them will not break backward compatibility.
If fixing this is of any interest, I can help by opening a pull request on github.

Best regards
Michele
Hi Michele,

Thanks for the report.

We are already aware of these issues:
https://github.com/tango-controls/cppTango/issues/497

https://github.com/tango-controls/cppTango/pull/396 already solved your problem with DeviceData and const string &.
This will be available in cppTango 9.3.3 coming out hopefully this week.

Sorry for the inconvenience.
Kind regards,

Reynald
Rosenberg's Law: Software is easy to make, except when you want it to do something new.
Corollary: The only software that's worth making is software that does something new.
I missed that issue. Thanks for the update. Indeed I haven't thought about ABI compatibility…

Best regards
Michele
 
Register or login to create to post a reply.