0

I am mocking a class A. It has 15 virtual functions that i want to override and call passed lambdas from that.

Code is:

struct A {
   virtual int funcA(int , int) ;
   virtual void funcB(double) ;
   // ... more..
};

Mock Class:

struct MockA : public A {
    std::function<decltype(funcA)> lambdaA;
    virtual decltype(((A*)nullptr)->funcA()) funcA(/* How to get aruments*/) override {
        return lambdaA(/* How to forward the arguments. */);
    }
};

I can not understand how to get arguments and forward them to lambdaA ?

Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
  • I don't think C++ allows extraction of method signatures. You'll probably need to use preprocessor tricks instead. – MooseBoys Apr 24 '15 at 07:27
  • ok. how to do that ? – Ashish Negi Apr 24 '15 at 07:27
  • You can create a method declaration helper macro, then use the `__COUNTER__` macro to create a series of macros that save the method definitions uniquely. Then you can use the approach [here](http://stackoverflow.com/questions/319328/writing-a-while-loop-in-the-c-preprocessor) to recursively define the mock class methods. Unless you have a lot of classes it's probably easier to just copy-paste to create your mock class. – MooseBoys Apr 24 '15 at 07:36
  • 1
    You may replace `((A*)nullptr)->funcA()` by `std::declval().funcA()`. – Jarod42 Apr 24 '15 at 08:34
  • @MooseBoys i am sorry to say.. but rather than using that macro i would write simple program for the task. – Ashish Negi Apr 24 '15 at 09:32
  • You may do something with function traits (a little bit improved compared to Boost's ones), variadic templates and the indices trick. But even then, you would probably need some more work and indirection and it would still choke on overloaded function (not overwritten ones). – Morwenn Apr 24 '15 at 09:57

0 Answers0