首页 > 解决方案 > 为什么我不能将 && 添加到 Ret (Args...) &?

问题描述

当我试图这样写我自己的时候decay_t

#include <utility>
template <class T>
struct auto_decay {
    auto operator () () noexcept {
        return std::declval<T>();
    }
};

template <class T> using decay_t = decltype( (decl_as<decay_impl::auto_decay< T >>())() );

并使用以下方法对其进行测试:

#include <type_traits>
int main() {
    static_assert(is_same<decay_t<int ()&>, int (*)()>{}());
}

我收到以下错误:

In file included from test_decay.cc:1:
In file included from ./../src/decay.hpp:4:
In file included from ./../src/utility.hpp:3:
./../src/add_reference.hpp:17:14: error: reference to function type 'int () &' cannot have '&' qualifier
    typedef T&& type;
             ^
./../src/add_reference.hpp:20:1: note: in instantiation of template class 'nxwheels::concept_check::add_rvalue_reference<int () &>' requested here
using add_rvalue_reference_t = typename add_rvalue_reference<T>::type;
^
./../src/utility.hpp:14:1: note: in instantiation of template type alias 'add_rvalue_reference_t' requested here
auto declval() noexcept -> add_rvalue_reference_t<T>;
^
./../src/decay.hpp:10:16: note: while substituting explicitly-specified template arguments into function template 'declval' 
        return declval<T>();
               ^
./../src/decay.hpp:16:46: note: in instantiation of member function 'nxwheels::concept_check::decay_impl::auto_decay<int () &>::operator()' requested here
template <class T> using decay_t = decltype( (decl_as<decay_impl::auto_decay< T >>())() );
                                             ^
test_decay.cc:17:17: note: in instantiation of template type alias 'decay_t' requested here
    assert_same<decay_t<int (void)&>, int (*)(void)>();
                ^
In file included from test_decay.cc:1:
./../src/decay.hpp:10:16: error: call to function 'declval' that is neither visible in the template definition nor found by argument-dependent lookup
        return declval<T>();
               ^
./../src/decay.hpp:16:46: note: in instantiation of member function 'nxwheels::concept_check::decay_impl::auto_decay<int () &>::operator()' requested here
template <class T> using decay_t = decltype( (decl_as<decay_impl::auto_decay< T >>())() );
                                             ^
test_decay.cc:17:17: note: in instantiation of template type alias 'decay_t' requested here
    assert_same<decay_t<int (void)&>, int (*)(void)>();
                ^
./../src/utility.hpp:14:6: note: 'declval' should be declared prior to the call site
auto declval() noexcept -> add_rvalue_reference_t<T>;
     ^
In file included from test_decay.cc:2:
./../src/concepts/is_same.hpp:18:5: error: static_assert failed
    static_assert(is_same_v<T1, T2>);
    ^             ~~~~~~~~~~~~~~~~~
test_decay.cc:17:5: note: in instantiation of function template specialization 'nxwheels::concept_check::assert_same<int, int (*)()>' requested here
    assert_same<decay_t<int (void)&>, int (*)(void)>();
    ^
3 errors generated.

为什么我不能&&添加Ret (Args...) &

标签: c++functiontypes

解决方案


int () &不是您(似乎)认为的那样。

它不是对函数的引用,而是一个ref 限定函数。

如果要引用函数,则需要使用int (&)().


为什么我不能添加&&Ret (Args...) &

似乎限定(ref-或 cv-qualified)函数类型只能在非常特定的地方使用:

[dcl.typedef]/6

具有cv-qualifier-seqref-qualifier的函数类型(包括由 typedef-name ( [dcl.typedef], [temp.param]) 命名的类型)应仅显示为:

-- 非静态成员函数的函数类型,

-- 指向成员的指针所指的函数类型,

-- 函数 typedef 声明或alias-declaration的顶级函数类型 ,

-- type-parameter的默认参数中的type-id,或

--类型参数( )的模板参数类型 ID 。[temp.arg.type]


推荐阅读