From 86db4fb7388285adc24e3aa6eb3f7ec68efd15f4 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 28 Jun 2016 17:14:21 +0200 Subject: Work around static initialization order issue value_traits::value_type initialization is not constexpr in VC because of pointers to function template instantiations (which apparently are not constexpr). --- build2/variable | 14 ++++++++++++++ build2/variable.cxx | 29 ++++++++++++++++++++++------- build2/variable.txx | 6 +++--- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/build2/variable b/build2/variable index adc11fa..a63b5b4 100644 --- a/build2/variable +++ b/build2/variable @@ -372,6 +372,13 @@ namespace build2 // // // static int compare (const T&, const T&); // + // // For simple types (those that can be used as elements of containers), + // // type_name must be constexpr in order to sidestep the static init + // // order issue (in fact, that's the only reason we have it both here + // // and in value_type.name -- value_type cannot be constexpr because + // // of pointers to function template instantiations). + // // + // static const char* const type_name; // static const build2::value_type value_type; // }; @@ -446,6 +453,7 @@ namespace build2 static name reverse (bool x) {return name (x ? "true" : "false");} static int compare (bool, bool); + static const char* const type_name; static const build2::value_type value_type; }; @@ -460,6 +468,7 @@ namespace build2 static name reverse (uint64_t x) {return name (to_string (x));} static int compare (uint64_t, uint64_t); + static const char* const type_name; static const build2::value_type value_type; }; @@ -477,6 +486,7 @@ namespace build2 static name reverse (const string& x) {return name (x);} static int compare (const string&, const string&); + static const char* const type_name; static const build2::value_type value_type; }; @@ -494,6 +504,7 @@ namespace build2 static name reverse (const path& x) {return name (x.string ());} static int compare (const path&, const path&); + static const char* const type_name; static const build2::value_type value_type; }; @@ -511,6 +522,7 @@ namespace build2 static name reverse (const dir_path& x) {return name (x);} static int compare (const dir_path&, const dir_path&); + static const char* const type_name; static const build2::value_type value_type; }; @@ -528,6 +540,7 @@ namespace build2 static name reverse (const abs_dir_path& x) {return name (x);} static int compare (const abs_dir_path&, const abs_dir_path&); + static const char* const type_name; static const build2::value_type value_type; }; @@ -545,6 +558,7 @@ namespace build2 static name reverse (const name& x) {return x;} static int compare (const name&, const name&); + static const char* const type_name; static const build2::value_type value_type; }; diff --git a/build2/variable.cxx b/build2/variable.cxx index 17a9d52..65bc023 100644 --- a/build2/variable.cxx +++ b/build2/variable.cxx @@ -333,9 +333,11 @@ namespace build2 throw invalid_argument (string ()); } + constexpr const char* const value_traits::type_name = "bool"; + const value_type value_traits::value_type { - "bool", + type_name, sizeof (bool), nullptr, // No base. nullptr, // No dtor (POD). @@ -371,9 +373,11 @@ namespace build2 throw invalid_argument (string ()); } + constexpr const char* const value_traits::type_name = "uint64"; + const value_type value_traits::value_type { - "uint64", + type_name, sizeof (uint64_t), nullptr, // No base. nullptr, // No dtor (POD). @@ -455,9 +459,11 @@ namespace build2 return s; } + constexpr const char* const value_traits::type_name = "string"; + const value_type value_traits::value_type { - "string", + type_name, sizeof (string), nullptr, // No base. &default_dtor, @@ -498,9 +504,11 @@ namespace build2 throw invalid_argument (string ()); } + constexpr const char* const value_traits::type_name = "path"; + const value_type value_traits::value_type { - "path", + type_name, sizeof (path), nullptr, // No base. &default_dtor, @@ -539,9 +547,11 @@ namespace build2 throw invalid_argument (string ()); } + constexpr const char* const value_traits::type_name = "dir_path"; + const value_type value_traits::value_type { - "dir_path", + type_name, sizeof (dir_path), nullptr, // No base, or should it be path? &default_dtor, @@ -570,9 +580,12 @@ namespace build2 return abs_dir_path (move (d)); } + constexpr const char* const value_traits::type_name = + "abs_dir_path"; + const value_type value_traits::value_type { - "abs_dir_path", + type_name, sizeof (abs_dir_path), &value_traits::value_type, // Assume direct cast works for both. &default_dtor, @@ -603,9 +616,11 @@ namespace build2 return names_view (&v.as (), 1); } + constexpr const char* const value_traits::type_name = "name"; + const value_type value_traits::value_type { - "name", + type_name, sizeof (name), nullptr, // No base. &default_dtor, diff --git a/build2/variable.txx b/build2/variable.txx index 31d9284..6d3121a 100644 --- a/build2/variable.txx +++ b/build2/variable.txx @@ -272,7 +272,7 @@ namespace build2 template const string value_traits>::type_name = string ( - value_traits::value_type.name) + 's'; + value_traits::type_name) + 's'; template const value_type value_traits>::value_type @@ -432,8 +432,8 @@ namespace build2 template const string value_traits>::type_name = string ( - value_traits::value_type.name) + '_' + - value_traits::value_type.name + "_map"; + value_traits::type_name) + '_' + + value_traits::type_name + "_map"; template const value_type value_traits>::value_type -- cgit v1.1