Mik*_*sar 8 c++ templates template-meta-programming variadic-templates c++11
在模板参数包中确定常用数字类型的最佳方法是:
可变参数模板(best_common_numeric_type)可以像这样使用:
template<typename... NumericTypes>
auto some_numeric_func(const NumericTypes&...)
-> typename best_common_numeric_type<NumericTypes...>::type;
Run Code Online (Sandbox Code Playgroud)
并具有以下实例:
[1] best_common_numeric_type<long, unsigned long, float, double, int>::type = double
[2] best_common_numeric_type<unsigned int, unsigned long>::type = unsigned long
[3] best_common_numeric_type<signed int, signed long>::type = signed long
[4] best_common_numeric_type<signed int, unsigned int>::type = signed long
[5] best_common_numeric_type<signed int, unsigned long>::type = int128_t (maybe)
Run Code Online (Sandbox Code Playgroud)
因此,例如,在情况[4]中,::type必须是signed long,因为signed int无法保持unsigned int没有溢出的风险,并且反过来unsigned int不能保持signed int没有下溢的风险.
这同样适用于[5],除了现在a signed long已不再足够,因为它无法保持unsigned long没有溢出的风险.
(实现可能是特定于数据模型的,但你明白了.)
那么在C++ 11中实现这一目标的最佳方式是什么呢?
您可以使用Boost Integer来选择正确的案例.
暂时忽略非整数元素类型的情况,这里是对提议案例的快速测试(GCC没有int128_t出现的情况):
#include <boost/mpl/vector.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/fold.hpp>
#include <boost/mpl/max_element.hpp>
#include <boost/integer.hpp>
#include <limits>
using namespace boost;
namespace best_fit_
{
// wrappers around Boost Integer http://www.boost.org/doc/libs/1_54_0/libs/integer/doc/html/boost_integer/integer.html#boost_integer.integer.sized
template <bool is_signed, int bin_digits> struct select_int;
template <int bin_digits> struct select_int<true, bin_digits> {
using type = typename boost::int_t<bin_digits + 1>::least;
};
template <int bin_digits> struct select_int<false, bin_digits> {
using type = typename boost::uint_t<bin_digits>::least;
};
// query helper
struct digits {
template <typename I> using apply = mpl::int_<std::numeric_limits<I>::digits>;
};
}
template <typename... I>
struct best_common_integral
{
private:
using Ints = mpl::vector<I...>;
using Bits = typename mpl::transform<Ints, best_fit_::digits>::type;
template <typename J>
struct is_signed { static constexpr bool value = std::numeric_limits<J>::is_signed; };
using max = typename mpl::deref<typename mpl::max_element<Bits>::type>::type;
// sigh, there is no `mpl::any`, AFAICT
using sign = typename mpl::fold<
Ints,
mpl::bool_<false>,
mpl::if_<is_signed<mpl::_2>, mpl::bool_<true>, mpl::_1>
>::type;
public:
using type = typename best_fit_::select_int<sign::value, max::value>::type;
};
#include <typeinfo>
#include <iostream>
#include <cassert>
int main()
{
using case1 = best_common_integral<long, unsigned long, float, double, int>;
using case2 = best_common_integral<unsigned int, unsigned long>;
using case3 = best_common_integral<signed int, signed long>;
using case4 = best_common_integral<signed int, unsigned int>;
using case5 = best_common_integral<signed int, unsigned long>;
//assert(typeid(case1::type) == typeid(double));
assert(typeid(case2::type) == typeid(unsigned long));
assert(typeid(case3::type) == typeid(signed long));
assert(typeid(case4::type) == typeid(signed long));
//assert(typeid(case5::type) == typeid(int128_t (maybe)));
}
Run Code Online (Sandbox Code Playgroud)
我参加派对有点晚了,这是我没有Boost的解决方案:
#include <type_traits>
#include <cstdint>
template<class I, bool Signed> struct mk_signed { typedef I type; };
template<> struct mk_signed<uint8_t , true> { typedef int16_t type; };
template<> struct mk_signed<uint16_t, true> { typedef int32_t type; };
template<> struct mk_signed<uint32_t, true> { typedef int64_t type; };
template<> struct mk_signed<uint64_t, true> { typedef int64_t type; };
template <typename... Ts> struct best_common_numeric_type;
template <typename T> struct best_common_numeric_type<T> { typedef T type; };
template <typename T, typename... Ts>
struct best_common_numeric_type<T, Ts...> {
typedef typename best_common_numeric_type<Ts...>::type TS;
typedef typename std::conditional < (sizeof(T) > sizeof(TS)), T, TS>::type bigger_integral;
constexpr static bool fp = std::is_floating_point<T>::value || std::is_floating_point<TS>::value;
constexpr static bool have_signed = !fp && (std::is_signed<T>::value || std::is_signed<TS>::value);
typedef typename std::conditional <
fp,
typename std::common_type<T,TS>::type,
typename mk_signed<bigger_integral,have_signed>::type
>::type type;
};
Run Code Online (Sandbox Code Playgroud)