addAndMakeVisible with variable number of arguments?


#1
ViewOptions()
{
	addAndMakeVisibleComponent(
			viewHeader, accountHeader,
			colourSaturation, colourSaturationLabel,
			passwordEditor, passwordLabel,
			usernameEditor,
			usernameLabel
	);
}

template <typename ComponentType>
void addAndMakeVisibleComponent(ComponentType & comp)
{
	addAndMakeVisible(comp);
}

template<typename ComponentType, typename... Args>
void addAndMakeVisibleComponent(ComponentType & comp, Args&... args) 
{
	addAndMakeVisible(comp);

	addAndMakeVisibleComponent(args...);
}

Might be really neat?


#2

Definitely a good idea - I’d thought of doing the same thing a few times!


#3

Not sure that’s a working example, might need some liberal & signs…


#4

Fixed. That last & sign was a bitch. Figured it would go after the …!!!


#5

What about an initializer_list instead? It might make the code more readable by adding the curly brackets.


#6

I can’t seem to find a way of making that work with references. I did try…some hints on stackoverflow that it’s not possible. See if you can do it, I’d be interested!


#7

It can’t work with references. Only with pointers, so you would need the ampersand symbol in front of every component… hmmmm


#8

Yeah exactly, figured that was way worse ;_)


#9

reference_wrapper any use?? And then can you iterate over an initializer list anyway?