Skip to content

Instantly share code, notes, and snippets.

@kikairoya
Created November 1, 2010 03:56
Show Gist options
  • Save kikairoya/657582 to your computer and use it in GitHub Desktop.
Save kikairoya/657582 to your computer and use it in GitHub Desktop.
#include <array>
template <typename T, typename ...Args>
inline std::array<T, sizeof...(Args)> make_array(Args &&...args) {
return std::array<T, sizeof...(Args)>{ std::forward<Args>(args)... };
}
#define MAKE_ARRAY(T, ...) decltype(make_array<T>(__VA_ARGS__)){__VA_ARGS__}
#include <typeinfo>
#include <iostream>
#include <algorithm>
struct object {
object(): value(0) { std::cout << "def-ctor\n"; }
object(int v): value(v) { std::cout << "ctor( " << value << " )\n"; }
object(object &&o) { std::cout << "move-ctor( " << o.value << " )\n"; value = o.value; o.value = -value; }
object(const object &o) { std::cout << "copy-ctor( " << o.value << " )\n"; value = o.value; }
object &operator =(object &&o) { std::cout << "move-assign( " << o.value << " to " << value << " )\n"; value = o.value; o.value = -value; }
object &operator =(const object &o) { std::cout << "copy-assign( " << o.value << " to " << value << " )\n"; value = o.value;}
~object() { std::cout << "destructor( " << value << " )\n"; }
int value;
};
int main() {
auto a = MAKE_ARRAY(object, object(1), object(2), object(3));
std::cout << typeid(a).name() << '\n';
std::for_each(a.begin(), a.end(), [](const object &o) {
std::cout << o.value << ", ";
});
std::cout << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment