this dir | view | cards | source | edit | dark top
#include<vector>
#include<string>

#include <cstdio>

int main(int argc, char** argv) {
    std::vector<std::string> arg(argv, argv + argc);
    
    for (int i = 0; i < argc; ++i) {
        // tohle nebude fungovat, protože arg[i] neopovídá typu %s
        std::printf("%d: %s\n", i, arg[i]);
    }
    
    return 0;
}


template<typename OP>
class binary : public abstop {
    public:
        binary(.... l, .... r) : left(std::move(l)), right(std::move(r)) {}
        virtual int eval() const override {
            return f(left->eval(), right->eval())
        }

    private:
        ..... left, right, OP f;
}
// chceme použít
binary<plus<int>>
// uvnitř bude
using my_type = typename OP::result_type; // nefunguje v C++20
// nebo
// chceme použít
binary<int, plus>
// to se dělá takhle
template<typename my_type, template<typename> typename GOP>
class binary : public abstrop<my_type> {
    using OP = GOP<my_type>;
}

class row_iterator {
    public:
        row_iterator &operator ++() {
            // něco
            return *this;
        }
        // tohle je prefixový ++
        // kdybychom chtěli postfixový, bylo by to s jedním intovým parametrem
        // (žádný intový argument se nepředává)
        // a musíme vracet hodnotou
        row_iterator operator ++(int) {
            row_iterator tmp = *this;
            ++*this;
            return tmp;
        }
        row_ref operator *() const { /* ... */ }
        // trikové řešení: do iterátoru zavřu row_ref
        // obvykle se z operátoru hvězdičky vrací T&
        
}
bool operator !=(const row_iterator& a, const row_iterator& b) { /* ... */ }
// můžeme implementovat spaceship operator


map<string, size_t> m;

if (m.exists(ident)) {
    id = m[ident];
    // neefektivní řešení, v mapě vyhledávám dvakrát
} else {
    m[ident] = id = m.size();
}
id = m.size();
auto [it, b] = m.emplace(ident, id);

if (!b) {
    id = it->second;
}
// b je true, pokud tam ten prvek ještě nebyl
// když se emplace nepovedl, tak je b false a iterátor míří na překážející prvek