I'm wanting a sanity check about this behavior.
libraryOne.a
class myBase
{
public:
myBase();
virtual ~myBase() {};
myBase(const myBase& m) = default;
myBase& operator=(const myBase& m)= default;
myBase(myBase&& m)= default;
myBase& operator=(myBase&& m)= default;
virtual void ImplementMe() = 0;
void AnotherFunction();
private:
std::vector<SomeOtherClass> m_data;
};
library2.a
class myDerived : public MyBase
{
public:
myDerived();
~MyDerived();
virtual void ImplementMe() override;
}
Observation
Using the above scenario, Imagine the function in the base class, AnotherFunction(), has a for loop defined:
for(auto d : m_data)
I casually notice that the loop is making copies. Doh! So I fix it. Also we're not changing it, so I make it const.
for(const auto& d : m_data)
Suddenly my application which consists of one executable linking in LibraryOne.a and Library2.a fails to link. The change to the for loop was the only change. The linking error is atrocious, but i deduce that I have removed all usage of the copy constructor from libraryOne.a so the compiler stripped it out. "= default" created the constructor inline and from what I understand inline functions which are not used can be removed. When the application went to link, Library2.a no longer found the copy constructor for the base class and failed.
I solved the problem by defining the constructors in the .cpp file with the rest of the class definitions.
Question
Is that the correct way to solve the problem? Is there a portable way to tell the compiler not to toss out the default constructors because a class may inherit from it in another library?
I'm using GCC 5.3.1 on ubuntu.
$ gcc --version
gcc (Ubuntu 5.3.1-14ubuntu2) 5.3.1 20160413
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Aucun commentaire:
Enregistrer un commentaire