Boost.Units: Is it possible to convert on assignment without casting?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Boost.Units: Is it possible to convert on assignment without casting?

Boost - Users mailing list
Hi.

I have two unit quantities defined as follow:
static const auto Hz  = boost::units::si::hertz;
static const auto KHz = boost::units::si::kilo * Hz;
using Hertz     = boost::units::quantity<decltype( Hz ), double>;
using Kilohertz = boost::units::quantity<decltype( KHz ), double>;

Assigning one type to another does not compile:
Kilohertz freqKhz = Kilohertz::from_value( 1.234 );
Hertz freqHz      = freqKhz;
>>> error: conversion from 'Kilohertz {aka boost::units::quantity<...>, double>}' to non-scalar type 'Hertz {aka boost::units::quantity<...>, double>}' requested [1]

Is this possible, should it be enabled or is it not supported?
I am using boost 01.67, Mingw 5.3 and MSVC 2017 (C++14)

The following options work as expected:
Hertz freq1 = static_cast<Hertz>( freqKhz );
Hertz freq2 = Hertz( freqKhz );

But this adds a bit more code to maintain and keep in sync, one thing one tries to avoid when using the library since one needs to keep the LHS and the RHS of the opertor= in sync. 

PS: The chrono library allows this type of conversion given there is no information lost (but for double as storage types this should not be the case)
std::chrono::seconds sec      = std::chrono::seconds { 10 };
std::chrono::nanoseconds nsec = sec;

[1] https://gcc.godbolt.org/z/0sTHQQ

Regards,
Carel 


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost.Units: Is it possible to convert on assignment without casting?

Boost - Users mailing list
On 1/14/19 12:50 AM, Carel Combrink via Boost-users wrote:
> Hi.

Stephane Watanabe can likely give a better answer.  But I'm going to
take a shot anyhow.  Note I made a presentation at CppCon 2015
https://www.youtube.com/watch?v=qphj8ZuZlPA&t=4s  The slides and demo
code are available at
http://www.rrsd.com/software_development/boost/CPPCon2015/CPPCon2015.zip

>
> I have two unit quantities defined as follow:
> static const auto Hz  = boost::units::si::hertz;
> using Hertz     = boost::units::quantity<decltype( Hz ), double>;

On the wrong track here.  Make a clearer distinction between quantities,
dimensions and and units. Also, I strongly recommend against using auto
in cases like this.  It's by explicitly specifying the types which leads
one to understand where he has made an error in units/quantities/dimensions.

Robert Ramey

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost.Units: Is it possible to convert on assignment without casting?

Boost - Users mailing list
In reply to this post by Boost - Users mailing list
AMDG

On 1/14/19 1:50 AM, Carel Combrink via Boost-users wrote:
> I have two unit quantities defined as follow:
> static const auto Hz  = boost::units::si::hertz;
> static const auto KHz = boost::units::si::kilo * Hz;
> using Hertz     = boost::units::quantity<decltype( Hz ), double>;
> using Kilohertz = boost::units::quantity<decltype( KHz ), double>;
>
> Assigning one type to another does not compile:
> Kilohertz freqKhz = Kilohertz::from_value( 1.234 );
> Hertz freqHz      = freqKhz;

This is not assignment.  It uses the copy constructor
and requires an implicit conversion (which is forbidden
by design).  Try direct construction, instead:

Hertz freqHz{freqKhz};

>>>> error: conversion from 'Kilohertz {aka boost::units::quantity<...>,
> double>}' to non-scalar type 'Hertz {aka boost::units::quantity<...>,
> double>}' requested [1]
>
> Is this possible, should it be enabled or is it not supported?
> I am using boost 01.67, Mingw 5.3 and MSVC 2017 (C++14)
> <snip>
>

In Christ,
Steven Watanabe
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users