Consider the following example:
template <class T>
class C
{
public:
C();
C(C&& rhs);
private:
T m_data;
};
template <class T>
C<T>::C()
: m_data(T())
{
}
template <class T>
C<T>::C(C&& rhs)
: m_data(rhs.data)
{
}
int main()
{
C<int> i;
}
The line : m_data(rhs.data)contains an error because it Cdoes not have a member with a name data. But none of the compilers I tried (gcc 5.2, clang 3.5.1) found this error.
But when I add the following line to the function main, the compiler detects an error:
C<int> j = std::move(i);
Why does the compiler not give an error in the first case?
Even if this particular function is not called, it can understand that it Cdoes not have a member with a name data.
Also, when I change the definition of a move constructor to the following:
template <class T>
C<T>::C(C&& rhs)
: m_data(rhs.data)
{
data = 0;
}
the compiler gives an error in the string data = 0;, but not on : m_data(rhs.data). Thus, the function is analyzed.