title: C++ templates: return value by type
body: <p>I'm learning C++ and templates to implement a configuration file reader (<a href="http://www.adp-gmbh.ch/cpp/chameleon.html" rel="nofollow">http://www.adp-gmbh.ch/cpp/chameleon.html</a>) and I’m trying to make a template for a class with a <code>std::string</code> as internal storage, which can return its internal value when assigning it to a variable (double, long double, float, string, uint16_t, uint32_t).</p>
  
  <p>Platform: Win10 with Visual Studio Community 2015 Update 1</p>
  
  <pre><code>class HybridType {
  public:
      HybridType() {};
      explicit HybridType(const std::string&amp; value) : internalStr(value) {}
      explicit HybridType(const char* value) : internalStr (std::string(value)) { }
  
      template&lt;typename T, typename = typename std::enable_if&lt;std::is_arithmetic&lt;T&gt;::value, T&gt;::type&gt;
      explicit HybridType(T numericValue) {
          std::stringstream strstr;
          strstr &lt;&lt; std::setprecision(std::numeric_limits&lt;T&gt;::digits10 + 1) &lt;&lt; numericValue;
          internalStr = strstr.str();
      }
  
      operator std::string() const { return internalStr; }
  
      template&lt;typename T&gt;
      operator T() const {
          if (std::is_same&lt;T, std::uint16_t&gt;::value) return std::stoi(internalStr);
          if (std::is_same&lt;T, std::uint32_t&gt;::value) return std::stoul(internalStr);
          if (std::is_same&lt;T, std::uint64_t&gt;::value) return std::stoul(internalStr);
          if (std::is_same&lt;T, double&gt;::value) return std::stod(internalStr);
          if (std::is_same&lt;T, long double&gt;::value) return std::stold(internalStr);
          if (std::is_same&lt;T, float&gt;::value) return std::stof(internalStr);
          return std::stoll(internalStr);
      }
  
  
  
      template&lt;typename T&gt; operator T*() { return internalStr.c_str(); }
  
  private:
      std::string internalStr;
  };
  </code></pre>
  
  <p>When using it, I do the following:</p>
  
  <pre><code>uint32_t test = HybridType("50");
  long double test2 = HybridType("50.0");
  </code></pre>
  
  <p>But when I compile this, I get a lot of warnings:</p>
  
  <pre><code>1&gt; warning C4244: 'return': conversion from 'double' to 'uint32_t',
  possible loss of data 1&gt;  see reference to function template
  instantiation 'HybridType::operator T(void) const&lt;uint32_t&gt;' being
  compiled 1&gt;          with 1&gt;          [ 1&gt;              T=uint32_t 1&gt; ] 
  1&gt; warning C4244: 'return': conversion from 'long double' to
  'uint32_t', possible loss of data 1&gt; warning C4244: 'return':
  conversion from 'float' to 'uint32_t', possible loss of data 1&gt;
  warning C4244: 'return': conversion from '__int64' to 'uint32_t',
  possible loss of data
  </code></pre>
  
  <p>I didn’t really understand why I have these warnings (compiler cannot choose the appropriate type), because when I output my variables they seem to have the correct value?</p>