Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether a container is stable

std::vector is an unstable container, i.e. by resizing the vector, iterators might become invalidated. In contrast, std::list or boost::container::stable_vector are stable containers which keep the iterators valid until the removal of the corresponding element.

Is there a way to check whether a given container is stable? For instance, if I have something like

template<template <typename A, typename B=std::allocator<A> > class T=std::list>
class Foo
{
}

Is it possible to allow only for stable containers and forbid the unstable ones?

like image 995
davidhigh Avatar asked May 23 '13 09:05

davidhigh


People also ask

How do you know if a container ship is stable?

The statical stability of ships is checked by comparing the righting-arm curve with the curves of heeling arms. A heeling arm is calculated by dividing a heeling moment by the ship displacement force.

How do you know if a container is healthy?

We can also see the health status by running docker ps. Notice under STATUS, the status is Up with (healthy) next to it. The health status appears only when a health check is configured.

How can I check my Docker status?

The operating-system independent way to check whether Docker is running is to ask Docker, using the docker info command. You can also use operating system utilities, such as sudo systemctl is-active docker or sudo status docker or sudo service docker status , or checking the service status using Windows utilities.

What is PID 1 in container?

PID 1 is special on linux, it is unkillable, meaning that it doesn't get killed by signals which would terminate regular processes. So, in a container, the first process that is started really must install handlers for SIGTERM , otherwise it will stick around.


1 Answers

I don't think there is anything available providing such information, but you could write your own trait. However, you will need to specialize it for every stable container that may be used, which is perhaps not an option.

#include <boost/container/vector.hpp>

#include <iostream>
#include <type_traits>
#include <list>
#include <vector>

template <template <typename...> class Container>
struct is_stable
    : std::false_type
{};

template <>
struct is_stable<std::list>
    : std::true_type
{};

template <>
struct is_stable<boost::container::stable_vector>
    : std::true_type
{};

template<template <typename...> class Container = std::list>
class Foo
{
    static_assert(is_stable<Container>::value, "Container must be stable");
};

int main()
{
    Foo<std::list> f1; // ok
    Foo<std::vector> f2; // compiler error
}

I don't think there is a way you can automatically detect that a container is stable, without resorting to manual specialization.


Just for fun, I tried writing what the concept/axiom for stability would look like (concepts and axioms are an extension to the language that were considered for inclusion in C++11):

concept StableGroup<typename C, typename Op>
    : Container<C>
{
    void operator()(Op, C, C::value_type);

    axiom Stability(C c, Op op, C::size_type index, C::value_type val)
    {
        if (index <= c.size())
        {
            auto it = std::advance(c.begin(), index);
            op(c, val);
            return it;
        }
        <->
        if (index <= c.size())
        {
            op(c, val);
            return std::advance(c.begin(), index);
        }
    }
}

If think this correctly captures the requirement that every iterator over the original container is equivalent to the corresponding iterator over the modified container. Not sure this is very useful, but coming up with such axioms is an interesting exercise :)!

like image 97
Luc Touraille Avatar answered Oct 13 '22 00:10

Luc Touraille