variant

Does something like a VB “Variant” implementation exist which uses C#'s dynamic dispatch?

强颜欢笑 提交于 2019-12-22 09:49:31
问题 I realize that it goes against the strongly typed nature of C#, but I find that when working with dynamic objects in the language, some of the more useful features typically found in JavaScript or PowerShell are simply not practical. For example, the following C# code will fail at runtime and it's obvious why. dynamic x = 1.0; int y = x; But that makes the dynamic features of C# pretty limited when dealing with loosely typed data such as that produced by JSON payloads or CSV where subtle

boost::variant conversion to type

青春壹個敷衍的年華 提交于 2019-12-22 04:09:45
问题 I have the following variant from the boost lib: typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant; Now I want to get a value from a variable declared as ' value ' in a struct node , so I thought I could work generic and call the function as such: find_attribute<long>(attribute); , however the compiler says it cannot cast from variant to long or any other type I give it. What am I doing wrong? template <typename T> T find_attribute(const std

Why does std::visit take a variable number of variants?

依然范特西╮ 提交于 2019-12-22 01:26:44
问题 Trying to get more familiar with C++17, I've just noticed std::visit: template <class Visitor, class... Variants> constexpr /*something*/ visit(Visitor&& vis, Variants&&... vars); Why does std::visit not take a single variant, but rather any number of variants? I mean, you can always take some standard library function and have it take multiple parameters with the same role, working on all of them (e.g. std::find() for multiple elements in a container); or you could be taking multiple

Is there a way to reset a std::variant from a known alternative?

落爺英雄遲暮 提交于 2019-12-21 07:34:51
问题 I'm in the process of updating a codebase that is currently using a custom equivalent of std::variant to C++17 . In certain parts of the code, the variant is being reset from a known alternative, so the class provides a method that asserts that index() is at a current value, but still directly invokes the proper destructor unconditionally. This is used in some tight inner loops, and has (measured) non-trivial performance impact. That's because it allows the compiler to eliminate the entire

Excel VBA: Variants in Array Variables

末鹿安然 提交于 2019-12-21 03:32:56
问题 A question on variants. Im aware that variants in Excel vba are both the default data type and also inefficient (from the viewpoint of overuse in large apps). However, I regularly use them for storing data in arrays that have multiple data types. A current project I am working on is essentially a task that requires massive optimistaion of very poor code (c.7000 lines)- and it got me thinking; is there a way around this? To explain; the code frequently stores data in array variables. So

How to build a SAFEARRAY of pointers to VARIANTs?

你。 提交于 2019-12-21 02:38:11
问题 I'm trying to use a COM component with the following method: HRESULT _stdcall Run( [in] SAFEARRAY(BSTR) paramNames, [in] SAFEARRAY(VARIANT *) paramValues ); How can I create in C/C++ the paramValues array? 回答1: Adding to the answers above for reference by future readers: In IDL, SAFEARRAY(...) means a pointer to an array descriptor. But in C++, SAFEARRAY means an array descriptor. So IDL's SAFEARRAY(...) is really C++'s SAFEARRAY * . This confused me to no end. To make things even more

How is duck typing different from the old 'variant' type and/or interfaces?

烈酒焚心 提交于 2019-12-20 08:09:17
问题 I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tell me, briefly: the difference between a 'duck type' and an old-skool 'variant type', and provide an example of where I might prefer duck typing over variant typing, and provide an example of something that i would have to use duck typing to accomplish? I don't mean to seem fowl by doubting the power of this 'new' construct, and I'm not

How is duck typing different from the old 'variant' type and/or interfaces?

不打扰是莪最后的温柔 提交于 2019-12-20 08:08:12
问题 I keep seeing the phrase "duck typing" bandied about, and even ran across a code example or two. I am way too lazy busy to do my own research, can someone tell me, briefly: the difference between a 'duck type' and an old-skool 'variant type', and provide an example of where I might prefer duck typing over variant typing, and provide an example of something that i would have to use duck typing to accomplish? I don't mean to seem fowl by doubting the power of this 'new' construct, and I'm not

variant double subtype exceed max value

为君一笑 提交于 2019-12-20 04:25:36
问题 when I'm looking at the "Variant Data Type" documantation, it says that variant with subtype of double can support a positive value of maximum "1.79769313486232E308" (15 digits) and that "An error occurs when Variant variables containing Currency, Decimal, and Double values exceed their respective ranges." However, when I'm running the following code: y = 999999999999999999999999999 y = CStr(CDBL(y)) MsgBox y I do not recive an error, instead I am getting a msgbox with the following output:

What is the equivalent of boost::variant in the C++ standard library?

三世轮回 提交于 2019-12-19 05:14:17
问题 I am looking for an alternative to C-style union. boost::variant is one such option. Is there anything in std C++ ? union { int i; double d; } 回答1: As several commenters said: No, there is no Boost Variant-alike in standard C++. Maybe in a few years there will be, but why wait--use Boost Variant today! Edit (four years later, 2016): In C++17 there will be std::variant . Similar but not identical to boost::variant . So when your compiler supports C++17, you will have a solution in the standard