C ++: using a class as a template argument before the class is fully defined

I find it difficult to understand why the following snippet compiles. I have a ptr_to_member<T> template class that stores a pointer to a member function of T Then I create a new class my_class that has ptr_to_member<my_class> . I expected the latter to cause a compilation error, given that my_class is still defined. Any help is appreciated. Thanks.

 #include <iostream> // this class simply stores a pointer to a member function of T template <class T> struct ptr_to_member { using ptr_type = void (T::*)(); ptr_type m_ptr; ptr_to_member() : m_ptr(&T::f){} auto get_ptr() const { return m_ptr; } }; // my_class has a ptr_to_member<my_class> class my_class { ptr_to_member<my_class> m_ptr_to_member; // why does this compile? public: void g() { auto ptr = m_ptr_to_member.get_ptr(); (this->*ptr)(); } void f() { std::cout << "f" << std::endl; } }; int main() { my_class x; xg(); } 
+5
source share
1 answer

When defining a class, this class can be used as if it were declared forward. Since ptr_to_member uses pointers only to T , until you create one of its methods, it does not complain. Try adding a T element instead of a pointer, and you will see that it does not work. An intuitive way to make sure you don’t need to know that T use a pointer T , only that T is a type. Pointers behave the same, no matter what they point to, until you spot them.

 template <class T> struct ptr_to_member { using ptr_type = void (T::*)(); ptr_type m_ptr; ptr_to_member() : m_ptr(&T::f) {} auto get_ptr() const { return m_ptr; } T member; // Add this and see that it fails to compile }; 

See this answer for more information on when you can and cannot use forward declared types.

0
source

Source: https://habr.com/ru/post/1262613/


All Articles