Properly accessing elements of std::vector::<std::reference_wrapper<Type>>

1
2
3
4
5
6
7
8
9
std::vector<std::reference_wrapper<Type>> vec;
//Some stuff...
for(int i = 0; i < vec.size(); i++)
{
	if(&vec[i] == &type)
	{
		return true;
	}
}


This is not working. I am completely new to std::reference_wrapper, so I guess I'm not sure to work with it. This above example gives me a 'lacks a cast'

I have another spot where I do something like:

 
vec[i].someMethod();


This gives me:

__gnu_cxx::__alloc_traits<std::allocator<std::reference_wrapper<Type> > >::value_type’ has no member named 'someMethod()'
Last edited on
You can pass them to functions that expect Type or Type& arguments, and you can use them to initialize Type& variables, but you can't call their Type members with the member access notation. It'd be vec[i].get().someMethod();
Awesome that did it. Thank you!

Any idea on the 'lacks a cast' error? It's pointing to the comparison there.

EDIT:
Nevermind, just needed to call get() there as well. Duh
Last edited on
you could also cast if you like, static_cast<Type&>(vec[i]).someMethod()
Topic archived. No new replies allowed.