C++ templates: return value by type -


i'm learning c++ , templates implement configuration file reader (http://www.adp-gmbh.ch/cpp/chameleon.html) , i’m trying make template class std::string internal storage, can return internal value when assigning variable (double, long double, float, string, uint16_t, uint32_t).

platform: win10 visual studio community 2015 update 1

class hybridtype { public:     hybridtype() {};     explicit hybridtype(const std::string& value) : internalstr(value) {}     explicit hybridtype(const char* value) : internalstr (std::string(value)) { }      template<typename t, typename = typename std::enable_if<std::is_arithmetic<t>::value, t>::type>     explicit hybridtype(t numericvalue) {         std::stringstream strstr;         strstr << std::setprecision(std::numeric_limits<t>::digits10 + 1) << numericvalue;         internalstr = strstr.str();     }      operator std::string() const { return internalstr; }      template<typename t>     operator t() const {         if (std::is_same<t, std::uint16_t>::value) return std::stoi(internalstr);         if (std::is_same<t, std::uint32_t>::value) return std::stoul(internalstr);         if (std::is_same<t, std::uint64_t>::value) return std::stoul(internalstr);         if (std::is_same<t, double>::value) return std::stod(internalstr);         if (std::is_same<t, long double>::value) return std::stold(internalstr);         if (std::is_same<t, float>::value) return std::stof(internalstr);         return std::stoll(internalstr);     }        template<typename t> operator t*() { return internalstr.c_str(); }  private:     std::string internalstr; }; 

when using it, following:

uint32_t test = hybridtype("50"); long double test2 = hybridtype("50.0"); 

but when compile this, lot of warnings:

1> warning c4244: 'return': conversion 'double' 'uint32_t', possible loss of data 1>  see reference function template instantiation 'hybridtype::operator t(void) const<uint32_t>' being compiled 1>          1>          [ 1>              t=uint32_t 1> ]  1> warning c4244: 'return': conversion 'long double' 'uint32_t', possible loss of data 1> warning c4244: 'return': conversion 'float' 'uint32_t', possible loss of data 1> warning c4244: 'return': conversion '__int64' 'uint32_t', possible loss of data 

i didn’t understand why have these warnings (compiler cannot choose appropriate type), because when output variables seem have correct value?

templates evaluated @ compile time, can't use if() select appropriate conversion function @ runtime (return types conflict, hence warnings).

you need provide type specialized implementations cast operator instead:

class hybridtype { public:     // ...     template<typename t>     operator t() const {         static_assert(std::is_same<t, std::uint16_t>::value ||                       std::is_same<t, std::uint32_t>::value                        // ...                      ,"casted unsupported type!");     }     // ... };  template<> hybridtype::operator std::uint16_t() const {     return std::stoi(internalstr); }  template<> hybridtype::operator std::uint32_t() const {     return std::stoul(internalstr); }  // aso. ... 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -