【问题标题】:type traits for result of operator overloaded for different ref-qualifiers为不同的引用限定符重载的运算符结果的类型特征
【发布时间】:2014-05-01 08:15:13
【问题描述】:

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

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>()));

};

但是在网上看到一个稍微不同的例子后(here):

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&lt;int, n&gt; 的快捷方式(参见上面关于符号的注释)):

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)。它们适用于左值和右值类型,但是对于非引用类型,它会返回对右值的调用。这是为什么呢?
有没有办法获得我想要的结果?我想要的正确吗?

我知道一个运算符为不同的限定符返回不同的类型不仅极其罕见,而且可能是一种不好的做法,但这并不意味着我不必了解我观察到的行为。 em>

【问题讨论】:

  • 顺便说一句,你可以写return {};...
  • @KerrekSB 谢谢,我想知道是否有更短的方法

标签: c++ c++11 language-lawyer typetraits


【解决方案1】:

你需要的是

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

放弃

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

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

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

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&lt;T&gt;(),因此您不需要std::forward&lt;T&gt;()

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 关键字)。 STL 容器中的成员 begin()end() 还不是那样(还),我不知道它们是否会出现以及何时出现,但我相信这是正确的方式。

我总是忽略const&amp;&amp; 的情况,因为我从未见过有意义的用法,而且我记得我读过 Stroustrup 说过同样的话。右值引用的想法正是能够修改一个临时对象(这是有道理的,因为这个对象可能持有指针或对实际数据的引用)。因此,const&amp;&amp; 似乎没有用,而 const&amp; 不会完全一样。

另一方面,人们也可以考虑使用volatile 限定符来保证完整性,它提供了四种组合,使总数增加到八种,仅此而已。但这种情况更为罕见。

【讨论】:

  • 您能否详细说明为什么Asgard&lt;ThorsChariot&gt;::Dereference 没有意义?这不是说Asgard&lt;int *&gt;::Dereference 没有意义吗?
  • 我同意const&amp;&amp;。我只是为了对称添加它
  • @bolov 这没有意义,因为&amp;&amp; 是由std::declval 添加的,正如我正在解释的那样。所以你得到的结果与Asgard&lt;ThorsChariot&amp;&amp;&gt;::Dereference 完全相同。
  • 我从接口的角度思考:Asgard&lt;ThorsChariot&gt;::Dereference 不应该是解引用左值的类型而不是解引用右值的类型吗?
  • @bolov 查看reference collapsing rulesT + &amp;&amp; 给出T&amp;&amp;T&amp; + &amp;&amp; 给出T&amp;。如果您按照您的说法添加&amp;,那么您总是 获得一个左值引用,并且您无法指定一个右值引用。 “左值总是赢”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多