C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Language support for type erasure

From: Marcin Jaczewski <marcinjaczewski86_at_[hidden]>
Date: Mon, 13 Apr 2026 15:04:08 +0200
pon., 13 kwi 2026 o 14:34 Jonathan Wakely via Std-Proposals
<std-proposals_at_[hidden]> napisał(a):
>
>
>
> On Mon, 13 Apr 2026 at 13:25, Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> wrote:
>>
>> And it is still possible to keep the Animal overload beside the new Dog and Cat ones. Fulfilling the requirements not to change its code.
>
>
> Exactly. The requirement is just that Cat and Dog do not inherit from Animal, and that print_name(cat) and print_name(dog) work. That's easy. You don't need to manually re-implement inheritance or introduce language support for type erasure.
>
> I hope the interviewer was looking for a simple solution, avoiding unnecessary overcomplication.
>

But if `print_name` was a more complicated function, like 50 lines?
you would duplicate it too?
It could be that calling current `print_name` was an implicit
requirement, but thinking out of the box sometimes is the right thing.

My solution (if current `print_name` needs to be called) is make it
POD object that is created from `cat` or `dog` and simply copy data.
Sometimes it could be beneficial in case integers it could have a lot
better performance than any indirection.

>
>
>>
>>
>>
>>
>>
>>
>> -----Ursprüngliche Nachricht-----
>> Von: Jonathan Wakely via Std-Proposals <std-proposals_at_[hidden]>
>> Gesendet: Mo 13.04.2026 14:13
>> Betreff: Re: [std-proposals] Language support for type erasure
>> An: C++ Proposals <std-proposals_at_[hidden]>;
>> CC: Jonathan Wakely <cxx_at_[hidden]>;
>>
>>
>> On Fri, 10 Apr 2026, 10:39 Frederick Virchanza Gotham via Std-Proposals, <std-proposals_at_[hidden]> wrote:
>>
>> In a job interview recently I was given the following code:
>>
>> #include <iostream>
>> #include <memory>
>> #include <string>
>>
>> struct Animal {
>> virtual ~Animal() = default;
>> virtual std::string name() const = 0;
>> };
>>
>> struct Cat : Animal {
>> std::string name(void) const override
>> {
>> return "Cat";
>> }
>> };
>>
>> struct Dog : Animal {
>> std::string name(void) const override
>> {
>> return "Dog";
>> }
>> };
>>
>> /* DO NOT CHANGE THE CODE BELOW! */
>>
>> void print_name(Animal const &a)
>> {
>> std::cout << a.name() << std::endl;
>> }
>>
>> int main(void)
>> {
>> Cat cat;
>> Dog dog;
>> print_name(cat);
>> print_name(dog);
>> }
>>
>> I was told to remove the inheritance, but I wasn't allowed to edit
>> code beneath the indicated line.
>>
>>
>> Or you could just add overloads of print_name taking Cat and Dog which works today and is simple to understand. That's the answer I would hope for if I asked that question.
>>
>> No std::function, no allocations, no hanging reference captures, no new language features needed. Just clear code.
>>
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>
>>
>> --
>> Std-Proposals mailing list
>> Std-Proposals_at_[hidden]
>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-04-13 13:04:27