C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Universal Variable Types C++ Addition

From: Jeremy Rifkin <rifkin.jer_at_[hidden]>
Date: Thu, 24 Apr 2025 21:09:34 -0500
 (re-sending on-list)
Hi,
In your initial email you mentioned a "universal variable type" that could
store "any value." The code you sent cannot do that.
You've found some clever tricks to try to serialize values of varying types
in the same storage, however, this ends up being way more complicated than
it needs to be. On top of that, it's also very inefficient, type unsafe,
and buggy .
A better way to implement what you have here would be
`std::variant<std::string, char, bool, int, float, double>`. You can still
define helper operator overloads or create a wrapper class around this with
helpful methods. E.g.:

class var {
    using storage_t = std::variant<std::string, char, bool, int, float,
double>;
    storage_t value;
public:
    var(std::string s) : value(std::move(s)) {}
    var(char c) : value(c) {}
    var(bool b) : value(b) {}
    var(int i) : value(i) {}
    var(float f) : value(f) {}
    var(double d) : value(d) {}

    template <typename T>
    T get() const {
        return std::get<T>(value);
    }

    // io, conversion, and operators implemented easily with whatever
semantics you'd like
};


Which types to support for something like this and what semantics you want
for conversion and operations is always very application-specific. As such,
this wouldn't be a good candidate for addition to the language and instead
should be left to applications.

Cheers,
Jeremy


On Thu, Apr 24, 2025 at 7:10 PM Paul Robert Stackhouse <
prstackhouse_at_[hidden]> wrote:

> Hello,
>
> I've just looked at the file for std::any, and it seems to operate
> differently than the classes that I've tried to create. The any does not
> seem to be able to accept string values, which my var and varDeque types
> can. My classes also can do so without the use of templates. The biggest
> downside, in addition to my varDeque class being incomplete, is that there
> are still a few bugs that I have to work out, mainly with the comparison
> (>, >=, etc) operators for the var class.
>
> Here are the files as I've created them. I converted them into txt files
> so that they can be safely sent to you.
>
> Despite not being template-based, they both store enough information in
> them to pass as variables. They store a double for the numerical value, and
> two strings for the text value and type value. The var type returns any
> value type, from string and char to bool, float, and double. It contains
> the operators for that. And once the varDeque is complete, it will be able
> to return any list or map of variables if one of those is what was stored
> inside them all along.
>
> Thank you for your responses.
>
> On Thu, Apr 24, 2025 at 7:31 PM Jeremy Rifkin <rifkin.jer_at_[hidden]>
> wrote:
>
>> Hi,
>> I'm slightly confused about your description and how you envision this
>> functionality working and what you're trying to achieve with it. As Jens
>> pointed out, this sounds a lot like std::any. The reason you might not have
>> heard of this or seen it used before is because it's of quite limited use
>> in C++. Unlike dynamic languages such as Python, C++ has no language
>> run-time that can handle dynamic types on the fly. Everything must be done
>> statically. While types like std::any exist and can let you store any
>> value, working with these can be quite cumbersome as you still have to
>> explicitly handle every possible type you care about.
>>
>> > I know how to use the yvals_core.h file to make a custom
>> std-namespace-based file
>>
>> I'm not quite sure what you mean by that but it sounds like something you
>> probably shouldn't be doing. It sounds like a good way to get UB.
>>
>> Cheers,
>> Jeremy
>>
>> On Thu, Apr 24, 2025 at 5:24 PM Paul Robert Stackhouse via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> Hello,
>>>
>>> I’m an up-and-coming programmer, who is making making their way through
>>> college. I am just about to complete an intro to C++ course. I have a few
>>> additions that I would like to propose. If needed, I can supply the code
>>> that I’ve worked on to make them a reality for myself.
>>>
>>> For some context, I know classes, templates, vectors, linked lists,
>>> recursion, etc. I know how to construct a variable from a class object and
>>> make it return different values by default, without using any class
>>> functions. I know about preprocessor directives, and I’ve seen some of the
>>> files for things like the iostream file, so I know how to use the
>>> yvals_core.h file to make a custom std-namespace-based file. That should
>>> provide you enough information to be able to see the capabilities of what
>>> I’ve made.
>>>
>>> As for what I’m recommending, it is a two-fold recommendation on making
>>> a universal variable type. The first is a universal value type. This can
>>> store any value inside itself and return any value stored in it to another
>>> variable. It is a universal value, since while it can store any individual
>>> value, it cannot store any structure of values. The other - and you’ll see
>>> how they are connected - is to make a universal storage type, that can
>>> store anything of any value inside itself. It would have to be able to
>>> store lists and vectors as well as maps, and it would have to be able to
>>> use only one set of storage for them. Additionally, as it would be a
>>> universal storage type, it would have to be able to store any individual
>>> values, like the universal value type. This would allow C++ to process
>>> complex and complicated documents with ease.
>>>
>>> Thank you for your consideration.
>>> paulrobert_at_[hidden]
>>>
>>> P.S.
>>> If the sender address looks like it does not match the actual email name
>>> of paulrobert_at_[hidden], that is intentional. I use the
>>> paulrobert_at_[hidden] name to create accounts, but any email sent
>>> to it gets redirected to the above email. I just thought I should let you
>>> know.
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>>

Received on 2025-04-25 02:09:48