Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) BoostUnitsExample17


Cpp98BoostQt CreatorLubuntuWindows


Boost.Units example 17 is a Boost.Units example.

Technical facts


Operating system(s) or programming environment(s)


Project type:

C++ standard:


Libraries used:






Qt project file: ./CppBoostUnitsExample17/CppBoostUnitsExample17.pro



SOURCES += main.cpp








#include <iostream>

#include <boost/units/io.hpp>
#include <boost/units/systems/si.hpp>
#include <boost/units/systems/si/prefixes.hpp>

namespace boost {
  namespace units {
    //These tags enable to recognize that sulfide molecules are not hydrogen molecules
    struct sulfide_molecule_amount_dimension_tag : base_dimension<boost::units::amount_base_dimension,1>{};
    struct hydrogen_molecule_amount_dimension_tag : base_dimension<boost::units::amount_base_dimension,1>{};

    typedef derived_dimension<sulfide_molecule_amount_dimension_tag,1>::type sulfide_molecule_amount_dimension;
    typedef derived_dimension<hydrogen_molecule_amount_dimension_tag,1>::type hydrogen_molecule_amount_dimension;
  } // namespace units
} // namespace boost

namespace boost {
  namespace units {
    namespace si {
      typedef unit<sulfide_molecule_amount_dimension,si::system> sulfide_molecule_amount;
      typedef unit<hydrogen_molecule_amount_dimension,si::system> hydrogen_molecule_amount;
    } // namespace si
  } // namespace units
} //namespace boost

int main()
  using boost::units::quantity;
  using boost::units::si::hydrogen_molecule_amount;
  using boost::units::si::sulfide_molecule_amount;
  using boost::units::si::hydrogen_molecules_mol;
  using boost::units::si::sulfide_molecules_mol;
  using SulfideMoleculeAmount = quantity<sulfide_molecule_amount>;
  using HydrogenMoleculeAmount = quantity<hydrogen_molecule_amount>;

  const SulfideMoleculeAmount s{1.0 * sulfide_molecules_mol};
  const HydrogenMoleculeAmount h{1.0 * hydrogen_molecules_mol};

    << "Number of hydrogen molecules: " << h << '\n'
    << "Number of sulfide molecules: " << s << '\n'
    //<< "Number of molecules: " << (s + h) << '\n' //Won't compile



Number of hydrogen molecules: 1 mol
Number of sulfide molecules: 1 mol
Number of molecules: 2 mol
Press <RETURN> to close this window...







Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml