Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    AlanU
    @AlanU
    void user()
    {
         vector<unique_ptr<Shape>> v;
         while (cin)
               v.push_back(read_shape(cin));
         for_all(v,[](Shape& s){ s.draw(); });       // draw_all()
         for_all(v,[](Shape& s){ s.rotate(45); });   // rotate_all(45)
    }
    
    Stroustrup, Bjarne. The C++ Programming Language . Pearson Education. Kindle Edition.
    AlanU
    @AlanU
    full list of opertiors of unique_ptr https://en.cppreference.com/w/cpp/memory/unique_ptr
    AlanU
    @AlanU
    AlanU
    @AlanU
    #include <iostream>
    #include <memory>
    #include <vector>
    using namespace std;
    
    class Shape {
    private:
        int m_id = 0;
    public:
        Shape(){}
        Shape(int id){ m_id = id;}
        Shape(const Shape &copy){ m_id = copy.m_id;}
        void draw(){ std::cout<<"object "<<m_id<<" draw"<<std::endl;}
        void rotate(unsigned int degree){ std::cout<<"object "<<m_id<<" rotate"<<degree<<std::endl;}
         // Shape(const Shape &&other);
        //void operator=(const Shape && other);
    };
    
    
    
    template<typename C, typename Oper>
    void for_all(C& c, Oper op)        // assume that C is a container of pointers
    {
         for (auto& x : c)
               op(*x);      // pass op() a reference to each element pointed to
    }
    
    void user()
    {
        std::vector<unique_ptr<Shape>> v;
        v.push_back(std::make_unique<Shape>(1));
         v.push_back(std::make_unique<Shape>(2));
         //while (cin)
              // v.push_back(read_shape(cin));
         for_all(v,[](Shape& s){ s.draw(); });       // draw_all()
         for_all(v,[](Shape& s){ s.rotate(45); });   // rotate_all(45)
    }
    
    
    int main()
    {
        user();
        cout << "Hello World!" << endl;
        return 0;
    }
      // pointer to contained data
        a = 1;
        int* i = std::any_cast<int>(&a);
        std::cout << *i << "\n”;
    AlanU
    @AlanU
    std::any a1 {1];
    if(a1.type() == typeid(int))
    //is int
    Lucas Anesti
    @LucasAnesti_gitlab
    hi
    AlanU
    @AlanU
    AlanU
    @AlanU
    Example of node with zero nodes
    #include <iostream>
    class Node
    {
    protected:
        Node * _right {nullptr};
        Node * _left {nullptr};
        std::string _key;
        static Node * zero() {
            static Node _zero {"zero"};
            _zero._left = & _zero;
            _zero._right = & _zero;
            return &_zero;
        }
    public:
        explicit Node(const std::string name) {
            _right = Node::zero();
            _left = Node::zero();
            _key = name;
        }
        Node() = delete ;
        void setLeft(Node & left) {
            _left = &left;
        }
        void setRight(Node & right) {
            _right = &right;
        }
    };
    
    int main()
    {
        Node root("root");
        Node left("left");
        Node right("right");
        root.setLeft(left);
        root.setRight(right);
        return 0;
    }
    AlanU
    @AlanU
    With Shared Pointer
    #include <iostream>
    #include <memory>
    class Node
    {
    protected:
        std::shared_ptr<Node> _right {nullptr};
        std::shared_ptr<Node> _left {nullptr};
        std::string _key;
        static std::shared_ptr<Node> zero() {
            static Node baseZero("zero");
            static std::shared_ptr<Node> _zero;
            _zero.reset(&baseZero);
            _zero->_left =  _zero;
            _zero->_right = _zero;
            return _zero;
        }
    public:
        explicit Node(const std::string name) {
            _right = Node::zero();
            _left = Node::zero();
            _key = name;
        }
    
        ~Node(){
            _right = nullptr;
            _left = nullptr;
        }
    
        Node() = delete ;
        void setLeft(std::shared_ptr<Node> left) {
            _left = left;
        }
        void setRight(std::shared_ptr<Node> right) {
            _right = right;
        }
    };
    
    int main()
    {
        Node root("root");
        root.setLeft(std::make_shared<Node>("left"));
        root.setRight(std::make_shared<Node>("right"));
        return 0;
    }
    Does this leak memory?
    #include <iostream>
    #include <memory>
    class Node
    {
    protected:
        std::shared_ptr<Node> _right {nullptr};
        std::shared_ptr<Node> _left {nullptr};
        std::string _key;
        static std::shared_ptr<Node> zero() {
            //Does this leak memory????
            static  std::shared_ptr<Node> _zero = std::make_shared<Node>("zero");
            _zero->_left =  _zero;
            _zero->_right = _zero;
            return _zero;
        }
    public:
        explicit Node(const std::string name) {
            _right = Node::zero();
            _left = Node::zero();
            _key = name;
        }
        ~Node(){
            _right = nullptr;
            _left = nullptr;
        }
        Node() = delete ;
        void setLeft(std::shared_ptr<Node> left) {
            _left = left;
        }
        void setRight(std::shared_ptr<Node> right) {
            _right = right;
        }
    };
    
    int main()
    {
        Node root("root");
        root.setLeft(std::make_shared<Node>("left"));
        root.setRight(std::make_shared<Node>("right"));
        return 0;
    }
    Grant Rostig
    @grantrostig
    if ( auto n = x.size()) {}
    if ( auto n = x.size(); n!= 0) {}
    AlanU
    @AlanU
    vector (InputIterator first, InputIterator last,
              const allocator_type& alloc = allocator_type());
    vector (initializer_list<value_type> il,
           const allocator_type& alloc = allocator_type());
    c++ 20
    more on what made it into C++ 20
    officer-tuba
    @officer-tuba
    #include <iostream>
    #include <variant>
    
    struct Expression { };
    struct Statement { };
    
    using Node = std::variant<Expression, Statement>;
    
    template <typename... Ts>
    struct overloaded : Ts...
    {
        using Ts::operator()...;
    };
    
    template <typename... Ts>
    overloaded(Ts...) -> overloaded<Ts...>;
    
    void check(Node p)
    {
        std::visit(overloaded {
            [](Expression& e) { std::cout << typeid(e).name() << "\n"; },
            [](Statement& s) { std::cout << typeid(s).name() << "\n"; }
        }, p);
    }
    
    int main(int, char const *[])
    {
        Expression exp;
        Statement stm;
    
        Node n_exp = exp;
        Node n_stm = stm;
    
        std::cout << "\n\n";
        check(n_exp);
        check(n_stm);
        std::cout << "\n\n";
    
        return 0;
    }
    AlanU
    @AlanU
    void check(Node &p)
    {
        std::visit(overloaded {
            [](auto& e) { std::cout << typeid(e).name() << "\n"; },
        }, p);
    }
    AlanU
    @AlanU
    // helper type for the visitor #4
    template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
    template<class... Ts> overloaded(Ts...) -> overloaded<Ts…>;
     std::vector<var_t> vec = {10, 15l, 1.5, "hello"}; 
    for (auto& v: vec) {
            // 4. another type-matching visitor: a class with 3 overloaded operator()'s
            std::visit(overloaded {
                [](auto arg) { std::cout << arg << ' '; },
                [](double arg) { std::cout << std::fixed << arg << ' '; },
                [](const std::string& arg) { std::cout << std::quoted(arg) << ' '; },
            }, v);
        }
    Class template argument deduction
    officer-tuba
    @officer-tuba
    explicit overloaded(Ts...) -> overloaded<Ts...>;
    AlanU
    @AlanU
      std::visit([](auto&& arg) {
                using T = std::decay_t<decltype(arg)>;
                if constexpr (std::is_same_v<T, int>)
                    std::cout << "int with value " << arg << '\n';
                else if constexpr (std::is_same_v<T, long>)
                    std::cout << "long with value " << arg << '\n';
                else if constexpr (std::is_same_v<T, double>)
                    std::cout << "double with value " << arg << '\n';
                else if constexpr (std::is_same_v<T, std::string>)
                    std::cout << "std::string with value " << std::quoted(arg) << '\n';
                else 
                    static_assert(always_false<T>::value, "non-exhaustive visitor!");
            }, w);
    officer-tuba
    @officer-tuba
    #include <iostream>
    #include <variant>
    
    struct Expression { };
    struct Statement { };
    
    using Node = std::variant<Expression, Statement>;
    
    template <typename... Ts>
    struct overloaded : Ts...
    {
        using Ts::operator()...;
    };
    
    template <typename... Ts>
    explicit overloaded(Ts...) -> overloaded<Ts...>;
    
    auto check(Node &p)
    {
        return std::visit(overloaded {
            [](auto& e) { return typeid(e).name(); },
        }, p);
    }
    
    int main(int, char const *[])
    {
        Expression exp;
        Statement stm;
    
        Node n_exp = exp;
        Node n_stm = stm;
    
        std::cout << "\n\n";
        std::cout << check(n_exp) << "\n";
        std::cout << check(n_stm) << "\n";
        std::cout << "\n\n";
    
        return 0;
    }
    ("std::visit is everything that is wrong with modern C++")
    Grant Rostig
    @grantrostig
    @AlanU , Impossible forward declarations of classes/structs? Only works for pointers to such objects, not the type itself? Please see if the two compile errors can be fixed. https://github.com/grantrostig/cpp_by_example/tree/master/forward_declarations
    AlanU
    @AlanU
    @grantrostig I need to do some reading with forward declarations. I think you are pretty limited when using them in main. I normal only used them in header files.
    Grant Rostig
    @grantrostig
    @AlanU , I did also use them in header files in that project. See "c.h" and "d.h".
    AlanU
    @AlanU
    i saw
    Grant Rostig
    @grantrostig
    I think the error and the fix (if it is even possible) would be the same for both main and the header files. I really do think that what I want to do is not possible. I know what I want the compiler to do, but it may be recursive in a way that the compiler doesn't like.