[#mp_units-value_cast-06]
= xref:mp_units.adoc[mp&lowbar;units]::value&lowbar;cast
:relfileprefix: ../
:mrdocs:

Explicit cast of a quantity&apos;s unit

== Synopsis

Declared in `&lt;mp&hyphen;units&sol;framework&sol;value&lowbar;cast&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;
    auto ToU,
    typename FwdQ,
    xref:mp_units/RoundingPolicy.adoc[RoundingPolicy] Policy = xref:mp_units/truncated_t.adoc[truncated&lowbar;t],
    xref:mp_units/Quantity-0c.adoc[Quantity] Q = std::remove&lowbar;cvref&lowbar;t&lt;FwdQ&gt;&gt;
requires UnitOf&lt;MP&lowbar;UNITS&lowbar;REMOVE&lowbar;CONST(decltype(ToU)), Q&colon;&colon;quantity&lowbar;spec&gt; &amp;&amp;
           detail&colon;&colon;ExplicitlyCastable&lt;Q&colon;&colon;unit, ToU, typename Q&colon;&colon;rep&gt; &amp;&amp;
           detail&colon;&colon;ValidRoundingPolicyFor&lt;Policy, typename Q&colon;&colon;rep, typename Q&colon;&colon;rep&gt;
&lsqb;&lsqb;nodiscard&rsqb;&rsqb;
constexpr
xref:mp_units/Quantity-0c.adoc[Quantity] auto
value&lowbar;cast(
    FwdQ&& q,
    Policy policy = Policy&lcub;&rcub;);
----

== Description

Implicit conversions between quantities of different types are allowed only for &quot;safe&quot; (e&period;g&period; non&hyphen;truncating) conversion&period; In truncating cases an explicit cast have to be used&period;

auto d &equals; value&lowbar;cast&lt;si&colon;&colon;second&gt;(1234 &ast; ms)&semi; auto e &equals; value&lowbar;cast&lt;si&colon;&colon;second&gt;(1567 &ast; ms, rounded)&semi;

== Return Value

the quantity converted to unit `ToU`

[NOTE]
====
The return value https://en.cppreference.com/cpp/language/attributes/nodiscard[should not be discarded^].
====
== Template Parameters

[cols="1,4"]
|===
| Name| Description
| *ToU*
| a unit to use for a target quantity
|===

== Parameters

[cols="1,4"]
|===
| Name| Description
| *q*
| the quantity to cast
| *policy*
| an optional rounding policy (`truncated` when not provided)
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
