1.. title:: clang-tidy - portability-template-virtual-member-function
2
3portability-template-virtual-member-function
4============================================
5
6Finds cases when an uninstantiated virtual member function in a template class causes
7cross-compiler incompatibility.
8
9Upon instantiating a template class, non-virtual member functions don't have to be
10instantiated unless they are used. Virtual member function instantiation on the other hand
11is unspecified and depends on the implementation of the compiler.
12
13In the following snippets the virtual member function is not instantiated by GCC and Clang,
14but it is instantiated by MSVC, so while the snippet is accepted by the former compilers,
15it is rejected by the latter.
16
17.. code:: c++
18
19    template<typename T>
20    struct CrossPlatformError {
21        virtual ~CrossPlatformError() = default;
22
23        static void used() {}
24
25        virtual void unused() {
26            T MSVCError = this;
27        };
28    };
29
30    int main() {
31        CrossPlatformError<int>::used();
32        return 0;
33    }
34
35Cross-platform projects that need to support MSVC on Windows might see compiler errors
36because certain virtual member functions are instantiated, which are not instantiated
37by other compilers on other platforms. This check highlights such virtual member functions.
38