用户定义类型的std :: common_type特征 [英] std::common_type trait for user defined types
问题描述
从C ++ 11开始,引入了类型特征 std :: common_type
。 std :: common_type
确定其所有模板参数之间的公共类型。在C ++ 14中,还引入了其帮助程序类型 std :: common_type_t
,以使代码使用 std :: common_type $ c $
std :: common_type
在重载算术运算符(例如,
template< typename T1,typename T2>
std :: common_type_t< T1,T2>运算符+(T1常量& t1,T2常量& t2){
返回t1 + t2;
}
如果其模板参数内置类型(例如, int
, double
)。但是,如果我向其提供用户定义类型作为模板参数,则似乎无法正常工作,例如
struct A {};
struct B {};
std :: common_type_t< A,B> //不起作用
Q :我怎么赚 std :: common_type
特性可以与用户定义的类型一起使用吗?
标准草案
有这样的写法:
如果至少有一个模板参数
,则程序可以专门化此特征。专业化是用户定义的类型。 [注意:在模板参数中只需要显式转换
时,需要
这样的专业化。 —尾注]
这意味着 std :: common_type
类型的专业化具有至少一种用户定义类型的特征是完全允许的。实际上,如果您查看§20.15.4.3common_type [time.traits.specializations] 的专业化,您会发现STL已经定义了 std :: common_type的专业化
用于用户定义的类型 std :: chrono :: duration
和 std :: chrono :: time_point
。
因此,使 common_type
为用户定义类型工作的正确方法是提供一个专门针对那些特定类型,例如
结构A {};
struct B {};
名称空间std {
template<>
struct common_type< A,B> {
使用类型= A;
};
}
在上面的代码示例中,我们指定<$ c $之间的公共类型c> A 和 B
是 A
。
Since C++11 the type trait std::common_type
was introduced. std::common_type
determines the common type between all of its template arguments. In C++14 its helper type std::common_type_t
was also introduce in order to make code that uses std::common_type
type trait shorter.
std::common_type
is particular useful in overloaded arithmetic operators, e.g.,
template<typename T1, typename T2>
std::common_type_t<T1, T2> operator+(T1 const &t1, T2 const &t2) {
return t1 + t2;
}
It works fine if its templates arguments are built in types (e.g., int
, double
). However I doesn't seem to work if I provide as template arguments to it user defined types e.g.,
struct A {};
struct B {};
std::common_type_t<A, B> // doesn't work
Q: How I can make std::common_type
trait work with user defined types?
According to the draft standard N4582 §20.13.2 Header synopsis [meta.type.synop] (Emphasis Mine):
The behavior of a program that adds specializations for any of the templates defined in this subclause is undefined unless otherwise specified.
Consequently, adding specializations for type_traits
can cause undefined behaviour unless somewhere else in the standard there's a wording for a specific type trait that supersedes the wording displayed above. Fortunately, in Table 60 - Other transformations:
There's the wording:
A program may specialize this trait if at least one template parameter in the specialization is a user-defined type. [ Note: Such specializations are needed when only explicit conversions are desired among the template arguments. — end note ]
This means that specializations of std::common_type
type trait that have at least one user-defined type are perfectly allowed. In fact if you take a look at §20.15.4.3 Specializations of common_type [time.traits.specializations] you'll find out that STL already defines specializations of std::common_type
for user defined types std::chrono::duration
and std::chrono::time_point
.
Thus, the correct way to make common_type
"work" for user defined types is to provide a specialization of it for those specific types, e.g.,
struct A {};
struct B {};
namespace std {
template<>
struct common_type<A, B> {
using type = A;
};
}
In the code example above we specify that the common type between A
and B
is A
.
这篇关于用户定义类型的std :: common_type特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!