Skip to content

Instantly share code, notes, and snippets.

@gintenlabo
Forked from kikairoya/make_array.cc
Created December 20, 2010 09:38
Show Gist options
  • Save gintenlabo/748202 to your computer and use it in GitHub Desktop.
Save gintenlabo/748202 to your computer and use it in GitHub Desktop.
#include <array>
#include <type_traits>
template <class T, class... Args,
class Result = std::array<typename std::decay<T>::type, sizeof...(Args)+1>
>
inline Result make_array( T && x, Args&&... args) {
return Result{ std::forward<T>(x), std::forward<Args>(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(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