I want to do a custom iterator for a game scene that contains characters in a std::vector. I can't use the iterator of a vector itself since, due to pooling reasons, not only it stores extra chunking data, but also not all values within vector are "valid". Of course, I could declare the type itself as a template argument (which I will do if nothing else works), but this iterator is very specific, and using it with other types will likely break things. However, I do need a reference to a scene itself, and that reference must be either const or non-const depending on the constness of an iterator itself. My first and only thought was to use enable_if, but it doesn't work for member variables, as I hoped it would
This is the mockup code I have so far so I don't have to compile the entire project just for this:
#include <iostream>
#include <type_traits>
struct IntContainer
{
int Value = 5;
}
template<bool Const = true>
class TestMember
{
public:
typename std::enable_if<(!Const), IntContainer&>::type CurContainer;
typename std::enable_if<Const, const IntContainer&>::type CurContainer;
template<std::enable_if_t<Const, bool> = true>
TestMember(const IntContainer& cur_container)
: CurContainer(cur_container)
{}
template<std::enable_if_t<(!Const), bool> = true>
TestMember(IntContainer& cur_container)
: CurContainer(cur_container)
{}
}
int main()
{
IntContainer cont;
TestMember<true>(cont);
std::cout << cont.CurContainer.Value;
TestMember<false>(cont);
std::cout << cont.CurContainer.Value;
}```
The error is in the attachment. Compiled with rextester's "C++ (gcc)"
How do?