Ужас под заголовком C++11 FAQ
От: Caracrist https://1pwd.org/
Дата: 17.10.11 12:15
Оценка:
здесь
    class Widget {    // Three alternative implementations represented as a union
    private:
        enum class Tag { point, number, text } type;    // discriminant
        union {        // representation
            point p;      // point has constructor
            int i;
            string s;     // string has default constructor, copy operations, and destructor
        };
        // ...
        widget& operator=(const widget& w)     // necessary because of  the string variant
        {
            if (type==Tag::text && w.type==Tag::text) {
                s = w.s;        // usual string assignment
                return *this;
            }

            if (type==Tag::text) s.~string();    // destroy (explicitly!)

            switch (type==w.type) {
            case Tag::point: p = w.p; break;    // normal copy
            case Tag::number: i = w.i; break;
            case Tag::text: new(&s)(w.s); break;    // placement new
            }
            type = w.type;
            return *this;
        }
    };


Я выделил не понятные мне места.

point p; — это какого типа переменная? с моей точки зрения это равнозначно написанию:
42 р;

new(&s)(w.s); — такое в операторе присваивания, это бесплатное пособие по UB
~~~~~
~lol~~
~~~ Single Password Solution
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.