Here is a toy example of something I am currently working on
class Foo
{
private:
std::string s;
std::vector<std::string> v;
public:
// Foo constructors and other big 3
std::string const& strmember() const;
std::vector<std::string> const& vecmember() const;
std::vector<std::string>& vecmember();
};
All good up till this point. The requirement comes such that I have to add overloads and change the guts of the Foo
as below
class Foo
{
private:
std::u16string ssname;
std::vector<std::u16string> vvname;
public:
// Foo constructors and other big 3
std::string const& strmember() const;
std::vector<std::string> const& vecmember() const;
std::vector<std::string>& vecmember();
std::u16string const& strmember(int) const;
std::vector<std::u16string> const& vecmember(int) const;
std::vector<std::u16string>& vecmember(int);
};
I have to keep older members and add overloads for the new so that older clients wont break. Appropriate character conversions are performed so that older constructors are delegated to new one and the members supplied are changed as per the new design.
In implement these members as below
std::string const& Foo::strmember() const
{
const auto& name = char_convert(ssname);
return name;
}
std::vector<std::string>& Foo::vecmember()
{
return vecmember();
}
std::vector<std::string>& Foo::vecmember()
{
std::vector<std::string> result;
std::transform(vvname.begin(),vvname.end(),std::back_inserter(result),[] (const std:u16string& in) -> std::string
{
return char_convert(in);
});
return result;
}
std::u16string const& Foo::strmember(int) const
{
return ssname;
}
std::vector<std::u16string> const& Foo::vecmember(int) const
{
return vvname;
}
std::vector<std::u16string>& Foo::vecmember(int)
{
return vvname;
}
When I tried to compile the changed code, compiler gives me following warning cum error
error: reference to local variable ‘result’ returned [-Werror=return-local-addr]
std::vector<std::string> result;
^~~~~~
foo.cpp: In member function ‘const string& foo::strmember() const’:
foo.cpp: error: function returns address of local variable [-Werror=return-local-addr]
return name;
^~~~
foo.cpp: note: declared here
const auto& name = char_convert(ssname);
^
cc1plus: all warnings being treated as errors
How do I solve this problem ? I can not change the interface as it might break unittests and clients.
How do I provide the mutator and accessor version of vecmember()
and strmember()
functions ?