Skip to content

Instantly share code, notes, and snippets.

@Fiona-J-W
Created July 19, 2015 14:42
Show Gist options
  • Save Fiona-J-W/c1bbe48f5167b89dd663 to your computer and use it in GitHub Desktop.
Save Fiona-J-W/c1bbe48f5167b89dd663 to your computer and use it in GitHub Desktop.
combining ctor
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
struct foo {
foo(int i): i{i} {}
int i = 0;
};
struct foos;
struct foos_ref {
foos_ref(const foos& ref): ptr{&ref} {}
const foos* ptr;
};
struct foos {
foos(foos&&) = default;
foos& operator=(foos&&) = default;
foos(std::initializer_list<foo> args): m_foos{args} {}
foos(std::initializer_list<foos_ref> args1, std::initializer_list<foo> args2 = {}):
foos{args2} {
for(const auto& x: args1) {
m_foos.insert(m_foos.end(), x.ptr->m_foos.begin(), x.ptr->m_foos.end());
}
}
std::vector<foo> m_foos;
};
int main() {
auto foo1 = foo{1};
auto foo2 = foo{2};
auto foo3 = foo{3};
auto foos1 = foos{foo1, foo2};
auto foos2 = foos{{foos1}, {foo3}};
auto foos3 = foos{foos2};
auto foos4 = foos{foos1, foos2};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment