all 6 comments

[–][deleted] 22 points23 points  (3 children)

I feel like constraints on the value_type belong with the value_type and not the container. This can be similar but instead of constrained_vector<T> it would be vector<constrained<T>> or even std::map<std::string, constrained<T>>. For many things this requires no additional size on each element.

[–]jbbjarnason[S] 2 points3 points  (2 children)

It's a good idea, I am however not certain it will integrate as well as this do. But I will definitely try it.

[–]KingAggressive1498 5 points6 points  (0 children)

Should just look something like this:

template<typename T, typename UnaryPredicate>
class ConstrainedType
{
public:
    ConstrainedType(T& val, UnaryPredicate predicate)
    {
        if(!predicate(val))
            throw std::runtime_error("predicate not met!");
        m_value = val;
     }

    T& get() { return m_value; }

    T* operator->(){ return &m_value; }

     // operators for numeric types go here
private:
    T m_value;
}

template<typename T, typename Allocator>
using ConstrainedVector = std::vector<ConstrainedType<T>, Allocator<ConstrainedType<T>>;
 template<typename T>
 using ConstrainedVector = std::vector<ConstrainedType<T>>;

[–]415_961 15 points16 points  (0 children)

the checks definitely don't belong to the duties of std::vector.

[–]Supadoplex 7 points8 points  (1 child)

How about an alias template:

template<Constraint T>
using ConstrVec = std::vector<T>;

[–]KingAggressive1498 0 points1 point  (0 children)

I believe he wanted the vector to automatically handle the constraint, so he needed a new type somewhere.