gpt4 book ai didi

c++ - 接受所有版本的 const/volatile 限定和 & vs && 的类模板特化

转载 作者:可可西里 更新时间:2023-11-01 16:42:34 24 4
gpt4 key购买 nike

我专攻std::common_type适合我的类型。我定义了以下专业:

common_type<my_type, my_type>

一切都很好。然后有人过来调用std::common_type<my_type, my_type &> .如果您传递引用与不传递引用(因为它在类型上调用 std::decay),则默认版本的行为相同。但是,它并不遵循 std::common_type 的非引用版本,我需要它才能正常工作。有没有比必须做这样的事情更好的方法(为简单起见,省略对 const 的右值引用):

common_type<my_type, my_type>
common_type<my_type, my_type &>
common_type<my_type, my_type const &>
common_type<my_type, my_type volatile &>
common_type<my_type, my_type const volatile &>
common_type<my_type, my_type &&>
common_type<my_type, my_type volatile &&>
common_type<my_type &, my_type>
common_type<my_type const &, my_type>
common_type<my_type volatile &, my_type>
common_type<my_type const volatile &, my_type>
common_type<my_type &&, my_type>
common_type<my_type volatile &&, my_type>
common_type<my_type &, my_type &>
common_type<my_type &, my_type const &>
common_type<my_type &, my_type volatile &>
...

当然有更好的方法吗?据我统计,如果我们忽略 const &&,则有 49 种可能的版本和 const volatile &&

注意:my_type本身其实就是一个类模板,所以specialize其实看起来更像

template<intmax_t lhs_min, intmax_t lhs_max, intmax_t rhs_min, intmax_t rhs_max>
class common_type<my_type<lhs_min, lhs_max>, my_type<rhs_min, rhs_max>>

结果是 my_type<min(lhs_min, rhs_min), max(lhs_max, rhs_max)>

如果我能完全控制主模板定义,解决方案会相当简单,但我显然无法更改 std::common_type .

最佳答案

据我所知,您不需要完全专门化二进制 common_type 的两边。这允许将一侧的特化数量减少到 12 个。如果您只需要 my_typemy_type 的特化之间的通用类型,那么在一侧进行特化就足够了。否则,您必须在右侧克隆它们,从而产生 24 个特化。

struct my_type;
struct unique_t;

#include <type_traits>

template<class L, class R, class = void>
struct mytype_common_type
{
// not many specializations are required here,
// as you can use std::decay and don't have to use "Exact Matches"
using type = unique_t;
};

namespace std
{
template<class T> struct common_type<my_type, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type const, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type volatile, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type const volatile, T>
: mytype_common_type<my_type, T> {};

template<class T> struct common_type<my_type&, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type const&, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type volatile&, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type const volatile&, T>
: mytype_common_type<my_type, T> {};

template<class T> struct common_type<my_type&&, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type const&&, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type volatile&&, T>
: mytype_common_type<my_type, T> {};
template<class T> struct common_type<my_type const volatile&&, T>
: mytype_common_type<my_type, T> {};
}

template<class T>
using Decay = typename std::decay<T>::type;

int main()
{
static_assert(std::is_same<unique_t,
std::common_type<my_type const volatile&&, int>::type
>{}, "!");
}

关于c++ - 接受所有版本的 const/volatile 限定和 & vs && 的类模板特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20166967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com