I am currently writing a library, which facilitates deferred calls and registration of callbacks, which must work using gcc, clang and MSVC.
I have encountered something that I find very strange. I have a function with two overloads and I get an error if and only if, the function is defined in the interface. I get the same error using gcc 6.3.0-18 and clang 3.8.1-24.
Create an interface following the recommendation by Google with a protected constructor.
#include <queue>
#include <memory>
template <class T>
class IA {
public:
virtual ~IA() = default;
// virtual bool push(const T& souce) = 0; /* Issue */
virtual bool push(T&& source) = 0;
protected:
IA() = default;
};
A class implementing the interface
template <class T>
class A : public IA<T> {
public:
~A() override {};
bool push(const T& source) {
m_queue.push(source);
return true;
}
bool push(T&& source) {
m_queue.push(std::move(source));
return true;
}
private:
std::queue<T> m_queue;
};
Now, if I instantiate the class using a std::unique_ptr
,
int main() {
A<std::unique_ptr<float> > a;
return 0;
}
Everything works fine. Unless, I uncomment the function with prototype bool push(const T& soruce)
from the interface. Then, I get the error.
/usr/include/c++/6/ext/new_allocator.h:120:4: error: use of deleted
function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const
std::unique_ptr<_Tp, _Dp>&) [with _Tp = float; _Dp =
std::default_delete<float>]’
{ ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
I am fully aware of the fact that std::unique_ptr
's cannot be copied, but why is the error not appearing if the function is present only in the implementation.