Jump to content

Iterating a variadic argument?


Josh
 Share

Recommended Posts

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);
	}

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

What does the `std::any args` data look like?

 

EDIT: I think I was completely wrong about the solution this message used to show

  • Haha 1

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

Link to comment
Share on other sites

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.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

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'
}
  • Like 1

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

Link to comment
Share on other sites

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++ :P

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

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...