因此,基本上,我正在编写一个模板,用于确定表达式的类型(在本例中是dereference操作符):
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)。它们适用于lvalue和rvalue类型,但是对于非引用类型,它会返回对rvalue的调用。为什么会这样呢?
有什么方法可以得到我想要的结果吗?我要的是对的吗?
我知道,为不同的限定符返回不同类型的操作符不仅非常罕见,而且可能是一种糟糕的做法,但这并不意味着我不必理解我观察到的行为。
发布于 2014-05-01 08:39:41
你需要的是
(E)
static auto check(T &&t) -> decltype(*std::forward<T>(t));和drop
Asgard<const ThorsChariot>::Dereference
Asgard<ThorsChariot>::Dereference从您的测试中,因为它们没有意义(意思是,它们等效于相应的rvalue引用)。这就给您留下了1 2 3 4,这正是其余四个测试的结果。
请记住,std::declval()总是向T添加一个&&引用,所以您得到的是一个rvalue引用,除非T是一个lvalue引用,在这种情况下,您也会得到一个lvalue引用。同样的事情发生在std::forward()上。
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关键字)。STL容器中的成员begin()、end()不是这样的(目前),我不知道它们是否和何时会这样,但我确信这是正确的方法。
我总是忽略const&&的情况,因为我从未见过有意义的用法,我记得我读过Stroustrup说过同样的话。rvalue引用的思想正是能够修改一个临时对象(这是有意义的,因为这个对象可能包含指针或对实际数据的引用)。因此,似乎没有使用const&&,在那里,const&不会做完全相同的事情。
在另一个极端,我们也可以考虑volatile限定符的完整性,它提供了四个更多的组合,将总数提高到8个,仅此而已。但这更罕见。
https://stackoverflow.com/questions/23404252
复制相似问题