テンプレート関数があるとしましょう:
template <typename A, typename B>
A fancy_cast(B)
{
return {};
}
使用目的はのようなものですfancy_cast<int>(1.f)。
ただし、ユーザーが2番目のテンプレートパラメータを手動で指定することを妨げるものは何もありませんfancy_cast<int, int>(1.f)。
typename B指定されないようにして、それを推論させるにはどうすればよいですか?
私はこれを思いつきました:
// Using this wrapper prevents the code from being
// ill-formed NDR due to [temp.res]/8.3
template <auto V> inline constexpr auto constant_value = V;
template <
typename A,
typename ...Dummy,
typename B,
typename = std::enable_if_t<constant_value<sizeof...(Dummy)> == 0>
>
A fancy_cast(B)
{
return {};
}
それは動作するように見えますが、それは非常に面倒です。もっと良い方法はありますか?
AやB?なぜそうならないのかわかりません。