#Constness of a member variable based on a bool template param

6 messages · Page 1 of 1 (latest)

versed forge
#

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?
vapid capeBOT
#

When your question is answered use !solved or the button below to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

merry quartz
#

templates are not my specialty but could you make a template specialization for true and false? a bit copy-pastey tbf but it works and lets both const and non-const members have the same name

fervent wharf
#

I don't fully understand the context, but just looking through the compile errors you have a couple of them that aren't related to TestMember and const/non-const stuff
Skipping those as well, my typical recommendation would be to introduce a type alias, and use that alias for everything
If you have access to c++20 I would also recommend looking at concepts/constraints, most uses of enable_if are better served with concepts instead

#
template<bool Const = true>
class TestMember
{
public:
    using ref2cont = std::conditional_t<Const, const IntContainer&, IntContainer&>;
    ref2cont CurContainer;
    
    TestMember(ref2cont cur_container) 
        : CurContainer(cur_container)
    {}
};
versed forge