C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Core language feature idea: structural typing support without templates

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Sat, 30 Aug 2025 07:56:12 +0200
On 30.08.25 07:40, Tek, Robert Mate via Std-Proposals wrote:
> If anyone has a better solution, please do let all of us know.
>
> The essence of the problem:
>
> ```cpp
> // Unwieldy.hpp
> class Unwieldy
> {
> public:
> // *One thousand members and methods*
> };
>
> // TestMe.hpp
> class TestMe
> {
> public:
> TestMe(Unwieldy& getRidOfMe, ...);
>
> private:
> Unwieldy& m_getRidOfMe;
> };
>
> // If only I could do this in the unit test:
> class MockUnwieldy
> {
> public:
> // About 10 required methods
> };
> #define Unwieldy MockUnwieldy
> #include "TestMe.hpp"
> // Oh no, linker error: TestMe.cpp was compiled against the original Unwieldy :'(
> ```

Thanks a lot for that use-case.

I think the ability to linker-diagnose such type mismatches is a feature (not a bug).
Once TestMe has been compiled, there should be no way to exchange its use of "Unwieldy"
for something else.

For the particular issue above, can you play with your -I include paths and add a -D
on the command line such that TestMe.cpp is compiled with MockUnwieldy under the hood?
Something like

  TestMe.cpp
  mocks/Unwieldy.hpp -> MockUnwieldy.hpp

  gcc -Imocks -DUnwieldy=MockUnwieldy -c TestMe.cpp

or so?

Jens

Received on 2025-08-30 05:56:26