考虑下面的例子,我挖掘了qazxsw poi
here on StackOverflow
其中 template<typename T, typename Pred>
T const & clamp ( T const& val,
typename boost::mpl::identity<T>::type const & lo,
typename boost::mpl::identity<T>::type const & hi, Pred p )
{
// assert ( !p ( hi, lo )); // Can't assert p ( lo, hi ) b/c they might be equal
return p ( val, lo ) ? lo : p ( hi, val ) ? hi : val;
}
阻止编译器根据第二个和第三个参数的类型推断T.这对我来说非常方便,但我不能使用typename boost::mpl::identity<T>::type
(请不要给我一点困难,因为这已经很难了)。
现在的问题是直接在标准库中是等价的,我找不到了?
C ++ 20将有Boost Library
。但你真的不必等待标准库来拥有它。它的整个实施是:
std::type_identity
template< class T >
struct type_identity {
using type = T;
};
template< class T >
using type_identity_t = typename type_identity<T>::type;
是一个相当直接的模板,只提供与提供的模板参数相同的boost::mpl::identity
。
它可以实现如下:
type
template <typename X>
struct identity
{
typedef X type;
};
的作品。 std::common_type_t<T>
:
如果From cppreference是1(即T ...只包含一种类型T0),则成员类型的名称与
sizeof...(T)
相同(如果存在);否则没有会员类型。
因此,std::common_type<T0, T0>::type
将为此工作