vendredi 1 juin 2018

How to capture the return value of timeout in linux

Could any one please tell me how to capture the o/p of timeout utility. As i have captured like below but it is not getting as expected.

std::string syscommand = "timeout -s KILL 1s command";
     int res = system(syscommand.c_str());
     std::cout << res;


As i gone through some post and i found o/p should be like below.

    124 if command times out
    125 if timeout itself fails
    126 if command is found but cannot be invoked
    127 if command cannot be found
    137 if command is sent the KILL(9) signal (128+9)
    the exit status of command otherwise

    and how to print the error no too.

Why wont std::bind work with a std::filesystem::path and std::ostream?

I am currently trying to write a program that makes use of std::bind with a std::filesystem::path and std::ostream, both as references, as follows:

#include <functional>
#include <filesystem>
#include <ostream>
#include <iostream>

struct Buggy{
    static void something(const std::filesystem::path &path, std::ostream &out);
    void bind();
};

void Buggy::bind(){
    auto function = std::bind(&Buggy::something, std::placeholders::_1, std::cout);
    function(std::filesystem::path("./"));
}

void Buggy::something(const std::filesystem::path &path, std::ostream &out){
    out << path.string() << std::endl;
}

int main(){
    Buggy buggy;
    buggy.bind();
}

I expect this code to simply output "./", but instead, it gives me massive template errors. Why is this? My use of std::bind looks correct to me. I am compiling with g++ --std=c++17 bug4.cpp -o bug4 -lstdc++fs on Linux.

I am unable to read the template errors because they are so intermixed with implementation details of this standard library. I have tried compiling with clang and gcc, both of which give similar errors. Searching via a search engine gives no useful results.

Invalid use of auto when declaring a variable C++, C++11

Whenever I try to compile my code I get an invalid use of auto. Coming from a Java background, I am not sure if I quite understand C++ templates. It seems like the compiler can infer the type. I am limited to C++11.

The error occurs in the GeneralRenderer.

Animal classes

class Animal {};

class Dog : public Animal {};

class Cat : public Animal {};

enum AnimalType {
    Dog,
    Cat
};

Render Classes

template<class T>
class AnimalRenderer {
    virtual void draw(T entity) = 0;
};

class DogRenderer : public AnimalRenderer<Dog> {};


class CatRenderer : public AnimalRenderer<Cat> {};

Logic

class GeneralRenderer {
public:
    void draw(
            std::map<AnimalType, std::vector<Animal>> animalMap) {

        for (auto entry: animalMap) { 
            AnimalRenderer<auto> renderer; // <--- Causes the error
            switch (entry.first) {
                case Dog:
                    renderer = DogRenderer();
                    break;
                case Cat:
                    renderer = CatRenderer();
                    break;
            }

            for (auto animal: entry.second) {
                renderer.draw(entry);
            }
        }
    };
};

A memory leak clears a variable

In the execution of the program, the class variable is erased.Please tell me what could be the problem? Example code:

class A
{
private:
    int x;
    int y;
    A(_x, _y)
    {
        x = _x;
        y = _y;
    }
}

with y everything is all right x here it is lost.

iterating over edges in boost reverse graph

I am unable to iterate over the list of edges in a reversed adjacency list graph. Minimal code sample listed below with corresponding clang 6/gcc 7.3 error messages. Any help will be greatly appreciated!!!

#include <boost/config.hpp>
#include <algorithm>
#include <vector>
#include <utility>
#include <iostream>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/reverse_graph.hpp>

#include <boost/graph/graph_utility.hpp>

using namespace boost;

struct vertexinfo {
int index{12};
};

struct edgeinfo {
int index{10};
};

using Graph = adjacency_list < vecS, vecS, bidirectionalS ,vertexinfo,edgeinfo>;
using ReversedGraph = reverse_graph<Graph>;

int main()
{
Graph graph(5);
auto e1 = add_edge(0, 2, graph);
graph[e1.first].index = 1;
auto e2 = add_edge(1, 1, graph);
graph[e2.first].index = 2;
auto e3 = add_edge(1, 3, graph);
graph[e3.first].index = 3;
auto e4 = add_edge(1, 4, graph);
graph[e4.first].index = 4;
auto e5 = add_edge(2, 1, graph);
graph[e5.first].index = 5;
auto e6 = add_edge(2, 3, graph);
graph[e6.first].index = 6;
auto e7 = add_edge(2, 4, graph);
graph[e7.first].index = 7;
auto e8 = add_edge(3, 1, graph);
graph[e8.first].index = 8;
auto e9 = add_edge(3, 4, graph);
graph[e9.first].index = 9;
auto e10 = add_edge(4, 0, graph);
graph[e10.first].index = 10;
auto e11 = add_edge(4, 1, graph);
graph[e11.first].index = 11;

ReversedGraph reversed_graph(graph);

std::cout << "original graph:" << std::endl;
print_graph(graph, get(vertex_index, graph));

std::cout << std::endl << "reversed graph:" << std::endl;
print_graph(reversed_graph, get(vertex_index, graph));

auto range = edges(reversed_graph);
std::for_each(range.first,range.second,[&](const auto& item){
    std::cout << reversed_graph[item].index << '\n';
});

return EXIT_SUCCESS;
}

error message In file included from prog.cc:9: /opt/wandbox/boost-1.67.0/clang-6.0.0/include/boost/graph/reverse_graph.hpp:149:14: error: binding value of type 'const boost::adjacency_list::edge_bundled' (aka 'const edgeinfo') to reference to type 'typename graph::detail::bundled_result, typename detail::get_underlying_descriptor_from_reverse_descriptor > >::type>::type' (aka 'edgeinfo') drops 'const' qualifier { return m_g[detail::get_underlying_descriptor_from_reverse_descriptor::convert(x)]; } ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ prog.cc:61:22: note: in instantiation of function template specialization 'boost::reverse_graph, const boost::adjacency_list &>::operator[] > >' requested here std::cout << reversed_graph[item].index << '\n'; ^ prog.cc:60:10: note: in instantiation of function template specialization 'std::__1::for_each >, boost::detail::undirected_edge_iter, void *>, boost::detail::edge_desc_impl, long>, boost::iterators::use_default, boost::iterators::use_default>, (lambda at prog.cc:60:44)>' requested here std::for_each(range.first,range.second,[&](const auto& item){

C++ template inheritance hides template parameters

template <typename>
struct B
{
    constexpr static int T = 5;
};

template <int T>
struct D : B<int>
{
    constexpr static int value = T;
};

int main()
{
    std::cout << D<7>::value << std::endl;
}

As I have recently learned that template parameters of a template derived class gets checked after the names in the base class scope during lookup. That being said, is there anyway to qualify value to refer to the template parameter T in the derived class?

Cannot Instantiate abstract class but class is not abstract / derived method's param

Why I can't create new instance of class which is derived class of my abstract base class? My derived class has overriden base method but with different parametr which is derived class of base class param.

class BaseArg {

} ;

class DerivedArg :public BaseArg{

} ;

class BaseHandler{
public:
virtual void handle(BaseArg* arg) = 0;
} ;

DerivedHandler {
void handle(DerivedArg* arg) {}
} ;


auto x = new DerivedHandler() ;

Compiler returns "Cannot Instantiate abstract class"

Why? In my opinion it should works.