#include <cstdio> // for printf.
#include <utility> // for move, forward.
#include <string> // for string.
namespace my_ns
{
namespace miruna
{
template<int var_id, typename var_type, typename... ctor_arg_types>
auto create_var(ctor_arg_types &&... args) -> var_type &
{
static var_type dummy{ std::forward<ctor_arg_types &&>(args)... };
return dummy;
}
}
// ここまで下準備
struct unko // テスト用
{
unko(double v, int i) { std::printf("create unko %lf, %d\n", v, i); }
unko(unko const &) { std::printf("copy\n"); }
unko(unko &&) { std::printf("move\n"); }
~unko() { std::printf("d-tor unkooo.\n"); }
};
namespace // よーしパパ(略)
{
int &g_i1 = miruna::create_var<1, int>(100);
int &g_i2 = miruna::create_var<2, int>(100);
int &g_i3 = miruna::create_var<3, int>(1000);
double &g_d4 = miruna::create_var<4, double>(2.71828);
unko &g_u5 = miruna::create_var<5, unko>(3.14, 8009);
std::string &g_u6 = miruna::create_var<6, std::string>("test string...");
}
}
///////////////////////////////
#include <iostream>
using namespace std;
auto main() -> int
{
using namespace my_ns;
cout << g_i1 << endl;
cout << g_i2 << endl;
cout << g_i3 << endl;
cout << g_d4 << endl;
cout << g_u6 << endl;
}
I2luY2x1ZGUgPGNzdGRpbz4JLy8gZm9yIHByaW50Zi4KI2luY2x1ZGUgPHV0aWxpdHk+CS8vIGZvciBtb3ZlLCBmb3J3YXJkLgojaW5jbHVkZSA8c3RyaW5nPgkvLyBmb3Igc3RyaW5nLgoKbmFtZXNwYWNlIG15X25zCnsKCW5hbWVzcGFjZSBtaXJ1bmEKCXsKCQl0ZW1wbGF0ZTxpbnQgdmFyX2lkLCB0eXBlbmFtZSB2YXJfdHlwZSwgdHlwZW5hbWUuLi4gY3Rvcl9hcmdfdHlwZXM+CgkJYXV0byBjcmVhdGVfdmFyKGN0b3JfYXJnX3R5cGVzICYmLi4uIGFyZ3MpIC0+IHZhcl90eXBlICYKCQl7CgkJCXN0YXRpYyB2YXJfdHlwZSBkdW1teXsgc3RkOjpmb3J3YXJkPGN0b3JfYXJnX3R5cGVzICYmPihhcmdzKS4uL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