eranpeer / FakeIt

C++ mocking made easy. A simple yet very expressive, headers only library for c++ mocking.
MIT License
1.22k stars 170 forks source link

Unit tests give compiler error C3640 with VS 2019/v142 for local classes with pure virtual #259

Closed malcolmdavey closed 1 year ago

malcolmdavey commented 2 years ago

When I use the latest VS compilers, I get this compiler issue with the Fakeit unit tests.

1>C:...\FakeIt\tests\miscellaneous_tests.cpp(55,26): error C3640: 'Miscellaneous::can_mock_class_with_protected_constructor::SomeClass::[thunk]: cdecl `public: void cdecl Miscellaneous::can_mock_class_with_protected_constructor(void)'::2'::SomeClass::vcall'{0,{flat}}' }'': a referenced or virtual member function of a local class must be defined

    void can_mock_class_with_protected_constructor()
    {
        struct SomeClass
        {
            virtual void foo() = 0; //  error C3640 ...
        protected:
            SomeClass(int)
            {
            }
        };
        Mock<SomeClass> mock;
        Fake(Method(mock, foo));
    }

https://docs.microsoft.com/en-us/cpp/error-messages/compiler-errors-2/compiler-error-c3640?view=msvc-170 Seems this will be issue with VS 2019 (v142 tool set) and later (VS 2022/v143)

// C3640.cpp
void f()
{
   struct S
   {
      virtual void f1();   // C3640
      // Try the following line instead:
      // virtual void f1(){}
   };
}
malcolmdavey commented 1 year ago

I guess this will continue to be an issue with VS, so I guess will just work around it. But have PR to fix it for the Fakeit unit tests, so we can test later compilers