Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created November 8, 2013 07:32
Show Gist options
  • Save mashiro/7367487 to your computer and use it in GitHub Desktop.
Save mashiro/7367487 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <boost/lexical_cast.hpp>
template <typename Result>
class manip
{
public:
typedef manip base_type;
typedef std::ios_base::fmtflags flags_type;
typedef Result result_type;
private:
flags_type format_;
flags_type mask_;
result_type value_;
public:
manip(flags_type format, flags_type mask)
: format_(format)
, mask_(mask)
{}
template <typename CharT>
friend std::basic_istream<CharT>& operator>>(std::basic_istream<CharT>& is, manip<Result>& v)
{
is.setf(v.format_, v.mask_);
is >> v.value_;
return is;
}
operator result_type() const { return value_; }
};
template <typename Result>
class hex : public manip<Result>
{
public:
hex()
: hex::base_type(std::ios::hex, std::ios::basefield)
{}
};
int main()
{
int x = boost::lexical_cast<int>("123");
std::cout << x << std::endl; // 123
int y = boost::lexical_cast< hex<int> >("0xffff");
std::cout << y << std::endl; // 65535
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment