C++ 不同ref限定符的运算符重载结果的类型特征

C++ 不同ref限定符的运算符重载结果的类型特征,c++,c++11,language-lawyer,typetraits,C++,C++11,Language Lawyer,Typetraits,因此,基本上我正在编写一个模板,用于确定表达式的类型(在本例中为解引用运算符): 以下是我测试的内容: Asgard<const ThorsChariot>::Dereference Asgard<ThorsChariot>::Dereference Asgard<const ThorsChariot &>::Dereference Asgard<ThorsChariot &>::Dereference Asgard<co

因此,基本上我正在编写一个模板,用于确定表达式的类型(在本例中为解引用运算符):

以下是我测试的内容:

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

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

Asgard<const ThorsChariot &&>::Dereference
Asgard<ThorsChariot &&>::Dereference
以下是不同尝试的结果:

(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
(A)
静态自动检查(T)->decltype(*std::declval());
4 4  4 4  4 4
(B)
静态自动检查(T)->decltype(*T);
2 2  2 2  2 2
(C)
静态自动检查(常量T&T)->decltype(*T);
1 1  1 1  1 1
(D)
静态自动检查(T&T)->decltype(*T);
1 2  1 2  1 2
(E)
静态自动检查(T&&T)->decltype(*std::forward(T));
静态自动检查(T&)->decltype(*std::declval());
3 4 1 2 3 4
(C) (D)我理解。(A) (B)给我奇怪的结果。
我得到的最接近的是使用完美转发(E)。它们适用于左值和右值类型,但是对于非引用类型,它返回对右值的调用。这是为什么?
有什么方法可以得到我想要的结果吗?我想要的对吗

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

您需要的是

(E)
static auto check(T &&t) -> decltype(*std::forward<T>(t));
顺便说一句,你的测试比必要的要复杂一些。您可以将
T
作为模板参数从
Dereference
传递到
check
,然后直接使用
std::declval()
,因此不需要
std::forward()

模板
Asgard结构{
模板

静态自动检查(int)->decltype(*std::declval());//顺便说一句,您可以编写
return{}
…@KerrekSB谢谢,我想知道是否有一个较短的方法可以解释一下为什么
Asgard::Dereference
没有意义?这不等于说
Asgard::Dereference
没有意义吗?我同意
const&
。我只是为了symetry@bolov这没有意义,因为&&
是由
std::declval
添加的。因此,您得到的结果与
Asgard::Dereference
完全相同。我从接口的角度思考:Asgard::Dereference不应该是对左值的解引用类型,而不是对右值的解引用类型吗?@bolov Look在:
T
+
&
给出了
T&
,而
T&
+
&
给出了
T&
。如果你像你说的那样添加
&
,那么你总是会得到一个左值引用,你无法指定右值引用。“左值总是赢”。
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
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
(E)
static auto check(T &&t) -> decltype(*std::forward<T>(t));
Asgard<const ThorsChariot>::Dereference
Asgard<ThorsChariot>::Dereference
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 );
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));

};