School notes / Charles University / Advanced C++

Czech notes

Lecture 7

Download Markdown · Download PDF

Drive byli templatovane jen funkce proto existuji takove ty make_pair fce

ted jsou i templatovane tridy.

da se i udelat, aby kdyz dostane konstruktor parametr, tak jak odvodi typ, ktery se pouzije pro ten objekt

template <typename T>
struct S {
  T value;
  S(T v) : value(v) {}
  S(const char* s) -> S<std::string>;
};

// pro array intu:

template <typename T, typename... U>
S(T, U...) -> S<std::array<T, 1 + sizeof...(U)>>;

Dedukce this:

class X{
    T& value;
    const T& get() const;
}

to pozna, ze ten this co se tam skryte posila, ze je const

abych nemusel psat const a neconst a l/r value varianty, muzu pouzit dedukci this:

class X{
    T& get() &; // pro lvalue
    T&& get() &&; // pro rvalue
    const T& get() const&; // pro const lvalue
    const T&& get() const&&; // pro const rvalue
}

muzu udelat:

class X{
    T& get(this X&); // pro lvalue
    T&& get(this X&&); // pro rvalue
    const T& get(this const X&); // pro const lvalue
    const T&& get(this const X&&); // pro const rvalue
}

coz mi dovoli naimplementovat funkci templaovanim.

class X{
    template <typename Self>
    auto&& get(this this Self&&){
        // implementace (je u vsech skoro stejna)
    }
}

inicializatory:

da se inicializovat:

std::vector<string> v = {"a", "b", "c"};
std::vector<string> v  ({"a", "b", "c"});
std::vector<string> v   {"a", "b", "c"};

kdyz inicializujeme pole, tak prekladac nejprve vyrobi anonimni lokalni pole - je lighweight a predava se hodnotou

je lokalni, takze nemuzeme z funkce vracet {1, 2, 3} - to dela nedefinovane chovani

Disinguished initializers:

struct S {
  int x;
  int y;
};
S s{.x = 1, .y = 2};
// misto
S s{1, 2};

pro pole se pouzivaji [] (pro enum mapovani poli)

std::enum Color { Red, Green, Blue };
std::string colorNames[] {
    [Red] = "Red",
    [Green] = "Green",
    [Blue] = "Blue"
};

Strukturalni typovani:

abychom nemuseli pouzivat dedicnost, tak doufame, ze dana trida bude mit stejne metody