Josh Posted April 14, 2023 Posted April 14, 2023 Every example I can find online uses a chr*. You can't call ++ on an std::any. Does anyone know how to correctly iterate this to get all the arguments?: bool LuaComponent::CallMethod(const WString& name, const std::any& args...) { std::vector<std::any> v; auto vargs = va_list(); va_start(vargs, args); while (args.has_value()) { v.push_back(args); args++;//??? this does not compile } va_end(vargs); return CallMethod(name, v); } Quote Let's build cool stuff and have fun.
IceBurger Posted April 14, 2023 Posted April 14, 2023 What does the `std::any args` data look like? EDIT: I think I was completely wrong about the solution this message used to show 1 Quote i now hate love C++ Beeeeeeeeeeeeeep~~This is a test of the emergency signature system~~Beeeeeeeeeeeeeep RX 6800XT | i5-13600KF | 32GB DDR5 | 1440p is perfect
IceBurger Posted April 14, 2023 Posted April 14, 2023 Would doing args = va_arg(vargs, std::any); instead of args++ work? Quote i now hate love C++ Beeeeeeeeeeeeeep~~This is a test of the emergency signature system~~Beeeeeeeeeeeeeep RX 6800XT | i5-13600KF | 32GB DDR5 | 1440p is perfect
Josh Posted April 15, 2023 Author Posted April 15, 2023 Apparently not. It's okay, I don't think I am going to use this anyways. Quote Let's build cool stuff and have fun.
Josh Posted April 16, 2023 Author Posted April 16, 2023 The C implementation of variadic functions is really bad. You have to specify the number of parameters, which makes it more code than just passing a vector. Quote Let's build cool stuff and have fun.
IceBurger Posted April 16, 2023 Posted April 16, 2023 edit: nvm, not even joking I thought it worked, but I built the wrong file. fml lmao. 🤡 edit 2: nvm again. this works (tbh idek if this is what you were trying to do to begin with): template <typename... Args> std::vector<std::any> VariadicToVector(Args&&... args) { return {std::forward<Args>(args)...}; } int main() { auto v = VariadicToVector(1, 'A', "hello", 2.8); std::cout << std::any_cast<char>(v[1]) << std::endl; // outputs 'A' } 1 Quote i now hate love C++ Beeeeeeeeeeeeeep~~This is a test of the emergency signature system~~Beeeeeeeeeeeeeep RX 6800XT | i5-13600KF | 32GB DDR5 | 1440p is perfect
IceBurger Posted April 16, 2023 Posted April 16, 2023 Looking back at your code, it appears that you wanted to exclude elements that don't have a value. This can be done like so: template <typename... Args> std::vector<std::any> VariadicToVector(Args&&... args) { std::vector<std::any> v; for (auto&& arg : std::initializer_list<std::any>{std::forward<Args>(args)...}) { if (arg.has_value()) { v.emplace_back(std::forward<decltype(arg)>(arg)); } } return v; } int main() { std::any a; // empty; aka has_value() == false auto v = VariadicToVector(1, a, 'A', "hello", 2.8); std::cout << std::any_cast<char>(v[1]) << std::endl; // outputs 'A', confirming exclusion } Don't you just love C++ Quote i now hate love C++ Beeeeeeeeeeeeeep~~This is a test of the emergency signature system~~Beeeeeeeeeeeeeep RX 6800XT | i5-13600KF | 32GB DDR5 | 1440p is perfect
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.