LLNL/units

std::cout stream error

githubuser0xFFFF opened this issue · 2 comments

The usage code from the documentation:

	using namespace units;
	measurement length1=45.0*m;
	measurement length2=20.0*m;

	measurement area=length1*length2;

	std::cout<<"the area is "<<area<< " or "<<area.convert_to(ft.pow(2))<<".\n";

Produces the following compile error (MinGW 7.3.0).

error: no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'units::measurement')
  std::cout<<"the area is "<<area<< " or "<<area.convert_to(ft.pow(2))<<".\n";
  ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~

Searching the code I have found the following line in units_decl.h

// std::ostream &operator<<(std::ostream &stream, units::unit u);

So does this mean, the stream operator is currently disabled and does not work or am I doing something wrong?

Thank you

I should remove that comment.

Anyway at one point there was a stream operator but it ended up causing a few issues and added a lot to the header files for not much value.

The recommended way of doing this is either add a stream operator yourself

std::ostream &operator<<(std::ostream &stream, units::measurement m)
{
     stream<< units::to_string(m);
     return stream;
}

or if it is just a one time use.

std::cout<<"the area is "<<to_string(area)<< " or "<<to_string(area.convert_to(ft.pow(2)))<<".\n";

depending on the compiler you might need to add units:: in front of to_string.

Thank you and yes, you should definitely remove the comment.

And thank you for this great library.