mardi 16 mars 2021

Boost optional conversion from string to uint problem

Could someone explain why the first conversion works and the second with optional does not ?

// Example program
#include <iostream>
#include <string>
#include <boost/optional.hpp>

int main()
{
  std::string x = "16";
  uint16_t t =  (uint16_t)std::stoi(x);
  std::cout<<t; // prints 16
  
  std::string x2  = "16";
  boost::optional<uint16_t> opt =  (uint16_t)std::stoi(x2);
  std::cout<<opt; // prints 1
}

Thank you.

Aucun commentaire:

Enregistrer un commentaire