I'm trying to compile code which specialize the function std::swap for my class. But I'm facing an issue that occurs because of rvalue constructor (it works when I comment the line)
I am compiling with g++ (GCC) 8.2.1 20180905 (Red Hat 8.2.1-3) and g++ -std=c++14 -Wall those options
#include <iostream>
#include <utility>
namespace ns1
{
class Foo
{
public:
Foo();
Foo(const Foo& that) {};
Foo(Foo&& that) {}; // <--- work when commented
~Foo();
void swap(Foo &that) {};
};
inline void swap(Foo &lhs, Foo &rhs)
{
lhs.swap(rhs);
}
} // namespace ns1
namespace std {
template <>
inline void swap(ns1::Foo &lhs, ns1::Foo &rhs)
{
lhs.swap(rhs);
}
} // namespace std
I have the following error message :
error: template-id 'swap<>' for 'void std::swap(ns1::Foo&, ns1::Foo&)' does not match any template declaration
inline void swap(ns1::Foo &lhs, ns1::Foo &rhs)
^~~~
In file included from /opt/rh/devtoolset-8/root/usr/include/c++/8/string:52,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/locale_classes.h:40,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/ios_base.h:41,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/ios:42,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/ostream:38,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/iostream:39,
from toto.cpp:1:
/opt/rh/devtoolset-8/root/usr/include/c++/8/bits/basic_string.h:6276:5: note: candidates are: 'template<class _CharT, class _Traits, class _Alloc> void std::swap(std::basic_string<_CharT, _Traits, _Alloc>&, std::basic_string<_CharT, _Traits, _Alloc>&)'
swap(basic_string<_CharT, _Traits, _Alloc>& __lhs,
^~~~
In file included from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/stl_algobase.h:64,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/bits/char_traits.h:39,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/ios:40,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/ostream:38,
from /opt/rh/devtoolset-8/root/usr/include/c++/8/iostream:39,
from toto.cpp:1:
The error you are seeing is because providing a user defined move-constructor prevents compiler from synthesizing move-assignment operator, therefore making your class non move-assignable.
As per cppreferece, T
in swap
must be move-assignable.
To fix it, provide move-assignment operator as well, demo
Foo& operator=(Foo&& other) {return *this;}
Foo& operator= (const Foo& other) { return *this; }
Also take a look at rule of five