fork download
  1. #include <cstdio> // for printf.
  2. #include <utility> // for move, forward.
  3. #include <string> // for string.
  4.  
  5. namespace my_ns
  6. {
  7. namespace miruna
  8. {
  9. template<int var_id, typename var_type, typename... ctor_arg_types>
  10. auto create_var(ctor_arg_types &&... args) -> var_type &
  11. {
  12. static var_type dummy{ std::forward<ctor_arg_types &&>(args)... };
  13.  
  14. return dummy;
  15. }
  16. }
  17.  
  18. // ここまで下準備
  19.  
  20.  
  21. struct unko // テスト用
  22. {
  23. unko(double v, int i) { std::printf("create unko %lf, %d\n", v, i); }
  24. unko(unko const &) { std::printf("copy\n"); }
  25. unko(unko &&) { std::printf("move\n"); }
  26. ~unko() { std::printf("d-tor unkooo.\n"); }
  27. };
  28.  
  29. namespace // よーしパパ(略)
  30. {
  31. int &g_i1 = miruna::create_var<1, int>(100);
  32.  
  33. int &g_i2 = miruna::create_var<2, int>(100);
  34.  
  35. int &g_i3 = miruna::create_var<3, int>(1000);
  36.  
  37. double &g_d4 = miruna::create_var<4, double>(2.71828);
  38.  
  39. unko &g_u5 = miruna::create_var<5, unko>(3.14, 8009);
  40.  
  41. std::string &g_u6 = miruna::create_var<6, std::string>("test string...");
  42. }
  43. }
  44.  
  45. ///////////////////////////////
  46.  
  47. #include <iostream>
  48. using namespace std;
  49. auto main() -> int
  50. {
  51. using namespace my_ns;
  52. cout << g_i1 << endl;
  53. cout << g_i2 << endl;
  54. cout << g_i3 << endl;
  55. cout << g_d4 << endl;
  56. cout << g_u6 << endl;
  57. }
  58.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
create unko 3.140000, 8009
100
100
1000
2.71828
test string...
d-tor unkooo.