Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <memory>
- #include <string>
- #include <iostream>
- using namespace std;
- struct A {
- virtual ~A() = default;
- A(int ba, string ca, string ga = "abcdefg"s)
- : b(ba)
- , c(ca)
- , g(ga)
- {
- }
- int b;
- string c;
- string g;
- };
- struct D : public A {
- D(int ba, string ca, bool ed, char fd)
- : A(ba, ca)
- , e(ed)
- , f(fd)
- {
- }
- bool e;
- char f;
- };
- class B {
- public:
- void Push(A* raw_data) {
- data_.emplace_back(raw_data);
- }
- const vector<unique_ptr<A>>& Get() const {
- return data_;
- }
- private:
- vector<unique_ptr<A>> data_;
- };
- class X {
- public:
- X(const B& b) : b_(b) {}
- ~X() = default;
- private:
- B b_; // static assertion failed: result type must be constructible from input type
- // const B& b_; // надо так
- };
- int main() {
- B b;
- A* a1 = new A(3, "three"s);
- A* d1 = new D(0, "zero"s, true, 'a');
- b.Push(a1);
- b.Push(d1);
- X compose_b(b); // static assertion failed: result type must be constructible from input type
- // потому что X хранит B b по значению; сам B имеет поле с unique_ptr, поэтому экземпляры B
- // некопируемые, а хранение по значению в X предполагает создание копии B, что невозможно
- }
Advertisement
Add Comment
Please, Sign In to add comment