不同引用限定符的运算符重载结果的类型特征

type traits for result of operator overloaded for different ref-qualifiers

本文关键字:运算符 重载 类型 特征 结果 引用      更新时间:2023-10-16

所以基本上我正在编写一个模板来确定表达式的类型(在本例中为取消引用运算符(:

template<class T>
struct Asgard {
  template <class T>
  static auto check(T) -> decltype(*std::declval<T>()); // <-- the important line
  static utils::tt::SubstFailure check(...);
  using Dereference = decltype(check(std::declval<T>()));
};

但是在网上看到一个略有不同的示例后(这里(:

template<class X>
static auto check(const X& x) -> decltype(x == x); // other operator, but not important.

这让我想到如果运算符针对对象的不同限定符重载怎么办,所以我创建了一个测试类:

struct ThorsChariot {
  std::integral_constant<int, 1> operator*() const &{
    return std::integral_constant<int, 1>();
  }
  std::integral_constant<int, 2> operator*() & {
    return std::integral_constant<int, 2>();
  }
  std::integral_constant<int, 3> operator*() const &&{
    return std::integral_constant<int, 3>();
  }
  std::integral_constant<int, 4> operator*() && {
    return std::integral_constant<int, 4>();
  }  
};

我使用的以下表示法基本上是指:

1 — the return type of a call on — const &
2                                  &
3                                  const &&
4                                  &&

这是我测试的内容:

Asgard<const ThorsChariot>::Dereference
Asgard<ThorsChariot>::Dereference
Asgard<const ThorsChariot &>::Dereference
Asgard<ThorsChariot &>::Dereference
Asgard<const ThorsChariot &&>::Dereference
Asgard<ThorsChariot &&>::Dereference

我认为这些类型应该是(按顺序((我使用 n 作为integral_constant<int, n>的快捷方式(请参阅上面关于符号的说明((:

1 2  1 2  3 4

以下是不同尝试的结果:

(A)
static auto check(T) -> decltype(*std::declval<T>()); 
4 4  4 4  4 4
(B)
static auto check(T t) -> decltype(*t);
2 2  2 2  2 2
(C)
static auto check(const T &t) -> decltype(*t);
1 1  1 1  1 1
(D)
static auto check(T &&t) -> decltype(*t);
1 2  1 2  1 2
(E)
static auto check(T &&t) -> decltype(*std::forward<T>(t));
static auto check(T &&) -> decltype(*std::declval<T>());
3 4 1 2 3 4
(

C(和(D(我理解。(A(和(B(给我奇怪的结果。
我得到的最接近的是使用完美转发(E(。它们适用于左值和右值类型,但对于非引用类型,它返回对右值的调用。这是为什么呢?
有没有办法获得我想要的结果?我想要的是正确的吗?

我知道运算符为不同的限定符返回不同类型的操作符不仅非常罕见,而且可能是一种不好的做法,但这并不意味着我不必理解我观察到的行为。

你需要的是

(E)
static auto check(T &&t) -> decltype(*std::forward<T>(t));

下降

Asgard<const ThorsChariot>::Dereference
Asgard<ThorsChariot>::Dereference

来自您的测试,因为它们没有意义(这意味着,它们等同于相应的右值引用(。这给你留下了1 2 3 4,这正是剩余四个测试的结果。

请记住,std::declval<T>()总是向T添加一个&&引用,所以你得到的是一个右值引用,除非T是一个左值引用,在这种情况下,你也会得到一个左值引用。同样的事情发生在std::forward<T>()

template< class T >
typename std::add_rvalue_reference<T>::type declval();
template< class T >
T&& forward( typename std::remove_reference<T>::type& t );
template< class T >
T&& forward( typename std::remove_reference<T>::type&& t );

顺便说一下,您的测试比必要的要复杂一些。您可以将T作为模板参数Dereference 传递到 check ,然后直接使用 std::declval<T>(),因此您不需要std::forward<T>()

template<class T>
struct Asgard {
  template <class T>
  static auto check(int) -> decltype(*std::declval<T>()); // <-- the important line
  template <class T>
  static utils::tt::SubstFailure check(...);
  using Dereference = decltype(check<T>(0));
};

(我认为也不需要int参数,但我现在不测试(。

现在

为不同的限定符返回不同的类型

现在可能很少见,但我觉得这是很好的做法,我认为将来会更频繁。例如,查看std::get,它可能是一个非成员函数,但同样可能是一个成员函数(这不是出于技术原因,例如必须编写template关键字(。成员begin(),STL容器中的end()不是那样的(还(,我不知道它们是否以及何时会这样,但我相信这将是正确的方式。

我总是忽略const&&的情况,因为我从未见过有意义的用途,我想我记得读过斯特劳斯特鲁普说同样的话。右值引用的想法正是为了能够修改临时对象(这是有意义的,因为该对象可能包含对实际数据的指针或引用(。因此,const&不会做完全相同的事情的地方似乎没有使用const&&

在另一个极端,人们也可以考虑完整性的volatile限定符,它给出了另外四个组合,将总数增加到八个,仅此而已。但这更是罕见。