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

Compilation error using Conformance mode in VS2022 #339

Open noujaimc opened 4 weeks ago

noujaimc commented 4 weeks ago

Hello,

during the upgrade of my project from Visual Studio 2019 to Visual Studio 2022, I encountered a compilation error related to the FakeIt library. Specifically, I’m using the single_header/mstest/fakeit.hpp header. When I enable conformance mode in VS2022, the following error occurs

C2440   'return': cannot convert from 'fakeit::MockingContext<void,const uint8_t [],int32_t,int32_t>' to 'fakeit::MockingContext<void,const uint8_t *,int32_t,int32_t>'     

Here is the code :

#include "pch.h"
#include "CppUnitTest.h"
#include "fakeit.hpp"

using namespace fakeit;
using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace UnitTest
{
  class IClass
  {
     public:
       virtual void TestMethod(const uint8_t buffer[], int32_t offset, int32_t count) = 0;
       virtual ~IClass() = default;
   };

  TEST_CLASS(UnitTest)
  {
     public:
       TEST_METHOD(TestMethod1)
        {
       Mock<IClass> mock;
           Fake(Method(mock, TestMethod));
        }
  };
}

fakeit

malcolmdavey commented 4 weeks ago

Don't know a fix for fakeit, but it's unusual to have an array parameter in C++. I've used VS up to 2022, but never done that.

If using an array, it's better to actually have it as a reference with an actual size. Also C++ allows reading the array size when templating const uint8_t (buffer&)[n], but then this may not suit a virtual method.

Not sure if changing this helps.

I guess the question is - why not otherwise have a pointer? Is it just trying to avoid a nullptr case?

noujaimc commented 3 weeks ago

I am aware that it's unusual to have an array parameter in C++, but I cannot simply change the signature of my code base for multiple reasons. I was able to compile every library I used in VS2022 except for this one. The code was compiling before in VS2019 with the conformance mode enabled.