jeudi 3 septembre 2015

C++ compiler does not detect error in class template

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;
}

Line : m_data(rhs.data) contains an error as C does not have a member named data. But none of the compilers that I tried (gcc 5.2, clang 3.5.1) detected that error.

But when I add the following line to main function the compiler detects the error:

C<int> j = std::move(i);

Why the compiler does not give an error in the first case?

Even if that particular function is not called it can figure out that C has no member named data.

Aucun commentaire:

Enregistrer un commentaire