variant

How to make a safer C++ variant visitor, similar to switch statements?

时光怂恿深爱的人放手 提交于 2019-12-02 20:14:50
The pattern that a lot of people use with C++17 / boost variants looks very similar to switch statements. For example: ( snippet from cppreference.com ) std::variant<int, long, double, std::string> v = ...; std::visit(overloaded { [](auto arg) { std::cout << arg << ' '; }, [](double arg) { std::cout << std::fixed << arg << ' '; }, [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; }, }, v); The problem is when you put the wrong type in the visitor or change the variant signature, but forget to change the visitor. Instead of getting a compile error, you will have the wrong

How to build this c++ typelist into a variant?

ⅰ亾dé卋堺 提交于 2019-12-02 19:24:11
问题 Here, how do I fix this c++ typelist template compile error? we built a typelist, using the code from modern c++ design. Question is now -- how do I take this and built it into a variant class? 回答1: /* * variant_modern.hpp * * Created on: Jun 4, 2010 * Author: vvenedik */ #ifndef _VARIANT_MODERN_HPP_ #define _VARIANT_MODERN_HPP_ struct NullType {} ; template <class T, class U> struct TypeList { typedef T Head; typedef U Tail; }; #define TYPELIST_1(T1) TypeList<T1, NullType> #define TYPELIST_2

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

∥☆過路亽.° 提交于 2019-12-02 14:22:44
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 ducking the issue by refusing to do the research, but I am quacking up at all the flocking hype i've

variant double subtype exceed max value

佐手、 提交于 2019-12-02 04:13:14
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: "1e+27" (27 is the number of digits in y). What is the explantion to this? how is "y" stored in the

c++ variant class member stored by reference

懵懂的女人 提交于 2019-12-02 04:11:13
I am trying to experiment with std::variant. I am storing an std::variant as a member of a class. In the below code, things work fine if the variant is stored by value, but does not work (for the vector case, and for custom objects too) if the variant is stored by reference. Why is that? #include <variant> #include <vector> #include <iostream> template<typename T> using VectorOrSimple = std::variant<T, std::vector<T>>; struct Print { void operator()(int v) { std::cout << "type = int, value = " << v << "\n"; } void operator()(std::vector<int> v) const { std::cout << "type = vector<int>, size =

How can I convert a string array to a variant array in VBscript?

白昼怎懂夜的黑 提交于 2019-12-01 21:07:06
I'm using a function in vbscript which returns a variant array of strings. JobIDs = objDoc.ConnectedSubmit(objServer) The problem is I can't get the Job ID values from that array, as vbscript doesn't handle typed variables. It just gives a type mismatch when I attempt to do ANYTHING with the JobIDs array. I found some promising information here , but when I used the conversion function: Set objConverter = CreateObject("ADS.ArrayConvert") ConvertedJobIDs = objConverter.CStrArray(JobIDs()) It is giving me the same type mismatch error. Am I missing something obvious here? This is, apparently, an

How to convert Array of bytes to Variant

放肆的年华 提交于 2019-12-01 20:25:54
How to convert a byte array to Variant? I have a WebService that should receive an array of byte, but it only accepts variable of type VARIANT, I wonder how to convert in order to pass it as parameter for Web Services. thank you According to the comment trail, you need to create a SAFEARRAY of bytes. Which is done like this in Delphi: V := VarArrayCreate([0, N-1], varByte); Or, if the SAFEARRAY needs 1-based indexing: V := VarArrayCreate([1, N], varByte); You can then populate the array in a loop using V[i] := ... . If you have a Delphi dynamic array of Byte , and the expected SAFEARRAY uses 0

trivially default constructible std::optional and std::variant

浪子不回头ぞ 提交于 2019-12-01 17:52:36
Is it permitable to design std::optional (currently std::experimental::optional ) in such a way, that for trivially default constructible type T corresponding std::optional< T > is also trivially default constructible? The same question regading std::variant and its integral discriminator. My own answer is: "No, it cannot be designed in this way, because value of its integral discriminator obtained during default initialization will be indeterminate if the object has automatic storage duration or if it is reinterpret_cast -ed from non-zero-initialized storage." Requirement to the user to do

trivially default constructible std::optional and std::variant

徘徊边缘 提交于 2019-12-01 17:24:29
问题 Is it permitable to design std::optional (currently std::experimental::optional ) in such a way, that for trivially default constructible type T corresponding std::optional< T > is also trivially default constructible? The same question regading std::variant and its integral discriminator. My own answer is: "No, it cannot be designed in this way, because value of its integral discriminator obtained during default initialization will be indeterminate if the object has automatic storage

How do you read from a multidimensional variant array returned from a COM object in PHP?

人盡茶涼 提交于 2019-12-01 09:27:02
I'm working with a COM object that returns a multidimensional VARIANT array (vt_array), and I'm trying to read values from the array. When I use print_r($mdArray) it displays variant Object . ( variant_get_type($mdArray) returns 8204 .) I tried using foreach ($mdArray as $oneArray) but I get the message: Warning: Loader::getfields() [loader.getfields]: Can only handle single dimension variant arrays (this array has 2) in C:\Inetpub\wwwroot\root\script\fileloader.php on line 135 Fatal error: Uncaught exception 'Exception' with message 'Object of type variant did not create an Iterator' in C: