C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extend std::type_info with more information

From: Phil Endecott <std_proposals_list_at_[hidden]>
Date: Thu, 11 Apr 2024 18:10:01 +0000
Arthur O'Dwyer wrote:
> On Thu, Apr 11, 2024 at 12:46 PM Phil Endecott via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>
>>
>> Let me describe the situation in which I discovered a need for a
>> std::any -like type that would allow me to cast to a base class:
>>
>> Consider a std::any used represent something like a "current selection" in
>> a GUI application.
>>
>> I have a variety of abstract base classes ("interfaces") that the
>> application's objects may implement, including whatever is currently
>> stored in the selection.
>>
>> User interface objects (such as menu items) may want to be enabled or
>> disabled depending on whether their action is appropriate for the current
>> selected object, and then invoke the action through the base class.
>>
>> So for example I may have an abstract base class like this:
>>
>> class Alignable
>> {
>> public:
>> enum Alignment { left, center, right };
>> virtual void align(Alignment a) = 0;
>> };
>>
>> Then I have concrete derived classes like this:
>>
>> class Paragraph: public Alignable, ....
>>
>> Then I have an alignment menu, which does something like this:
>>
>> align_menu.enabled =
>> any_base_cast<Alignable>(&current_selection) != nullptr;
>>
>> align_menu.left.on_tap =
>> any_base_cast<Alignable>(&current_selection) -> align(left);
>> ...
>>
>
> The Classic C++ way to do this is to define an abstract base class
> "Selectable" representing things-that-can-be-selected.
> That is, I assume it's not part of your software design to allow
> `current_selection` to hold an `int` or a `std::string`, but only some kind
> of application object (traditionally known as a "widget" or "window").

Well maybe they can select a string. If I want to support selecting
strings, do I have to define my own SelectableString that inherits
from Selectable and string, just to do this your way? Why can't I choose
to keep a normal std::string in my selection, and use std::any?

I'm not going to discuss this at length; sorry, I don't have the bandwidth.


Regards, Phil.

Received on 2024-04-11 18:10:03