variant

boost::variant conversion to type

喜欢而已 提交于 2019-12-05 02:48:39
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::string& attribute) { std::vector<boost::shared_ptr<node> >::iterator nodes_iter = _request->begin();

Changing active member of union in constant expressions

自闭症网瘾萝莉.ら 提交于 2019-12-05 01:00:48
问题 Playing with constexpr and union I found out, that I can't change active member of an union in constexpr . Just one exception: union of empty classes. constexpr bool t() { struct A {}; struct B {}; union U { A a; B b; } u{}; u.a = A{}; u.b = B{}; return true; } static_assert(t()); constexpr bool f() { struct A { char c; }; struct B { char c; }; union U { A a; B b; } u{}; u.a = A{}; u.b = B{}; // error originating from here return true; } static_assert(f()); First function may produce constant

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

泄露秘密 提交于 2019-12-04 22:30:37
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 visitors and using them on the same variant. So, why this specific 'variadification'? To make multiple

How to pass a variant object type 16396 in a COM method that requires an input value of a VT_VARIANT [12]

房东的猫 提交于 2019-12-04 21:48:45
I have in my PHP code, a COM object '$com_myObject' with a method 'myObjectMethod' which after I run com_print_type info($com_myObject); on it, it shows that it has the method 'myObjectMethod' declared as shown below myObjectMethod(/* VT_VARIANT [12] [in] */ $RawData) { /* Processes entered object data */ } In my code I am having another function return a variant object '$myInputObject' of type value 16396. I plan to use '$myInputObject' as the input value for 'myObjectMethod' method. print variant_get_type($myInputObject); //returns 16396 I am retrieving the value for '$myInputObject' as

Unsafe, `noexcept` and no-overhead way of accessing `std::variant`

[亡魂溺海] 提交于 2019-12-04 17:50:03
问题 std::variant provides the following access functions: std::get_if: take pointer to variant , return pointer to alternative. template <std::size_t I, typename... Ts> auto* std::get_if(std::variant<Ts...>* pv) noexcept; If pv is not a null pointer and pv->index() == I , returns a pointer to the value stored in the variant pointed to by pv . Otherwise, returns a null pointer value. This means that get_if 's implementation roughly looks like this: template <std::size_t I, typename... Ts> auto*

Using std::visit on a class inheriting from std::variant - libstdc++ vs libc++

ⅰ亾dé卋堺 提交于 2019-12-04 16:50:21
问题 Consider the following code snippet: struct v : std::variant<int, std::vector<v>> { }; int main() { std::visit([](auto){ }, v{0}); } clang++ 7 with -stdlib=libc++ -std=c++2a compiles the code; g++ 9 with -std=c++2a fails to compile the code, with the following error: /opt/compiler-explorer/gcc-trunk-20180711/include/c++/9.0.0/variant:94:29: error: incomplete type 'std::variant_size' used in nested name specifier inline constexpr size_t variant_size_v = variant_size<_Variant>::value; ^~~~~~~~~

How can I marshall between XLOPER and VARIANT? [closed]

北战南征 提交于 2019-12-04 14:50:50
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . I'm working on an Excel plugin (XLL), which communicates with COM objects. So, I have to marshall between XLOPER and VARIANT. I've got most of this working, but arrays are definitely a pain. I need to support 1- and 2D arrays. I imagine someone has already had to deal with this before. What's the best way to simplify dealing with VARIANT, SAFEARRAY, and XLOPER (and XLOPER12)? I had to hand-roll my own

How to define a variant<x,y,z> extracting subtypes of a template parameter

笑着哭i 提交于 2019-12-04 13:21:16
I am building a state-machine where state transitions are described as a variant, i.e.: using table = std::variant< /* state event followup-state */ transition<start, success<sock>, connecting>, transition<start, exception, failed>, transition<connecting, success<>, connected>, transition<connecting, exception, failed>, transition<connected, exception, failed> >; and transition being a simple type: template <typename ENTRY_STATE, typename EVENT, typename NEXT_STATE> struct transition { using entry_state = ENTRY_STATE; using event = EVENT; using next_state = NEXT_STATE; }; The state classes are

Function which returns an unknown type

99封情书 提交于 2019-12-04 10:53:06
问题 class Test { public: SOMETHING DoIt(int a) { float FLOAT = 1.2; int INT = 2; char CHAR = 'a'; switch(a) { case 1: return INT; case 2: return FLOAT; case 3: return CHAR; } } }; int main(int argc, char* argv[]) { Test obj; cout<<obj.DoIt(1); return 0; } Now, using the knowledge that a = 1 implies that I need to return an integer, etc., is there anyway Doit() can return a variable of variable data type? Essentially, with what do I replace SOMETHING ? PS: I'm trying to find a an alternative to

How to convert Array of bytes to Variant

跟風遠走 提交于 2019-12-04 04:14:39
问题 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 回答1: 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