Почему я не могу добавить && к Ret (Args ...) &? - PullRequest
0 голосов
/ 23 июня 2018

Когда я пытался написать свой собственный 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...) &?

1 Ответ

0 голосов
/ 23 июня 2018

int () & это не то, что вы (кажется) думаете, это.

Это не ссылка на функцию, а ссылка с квалификацией функция.

Если вы хотите ссылку на функцию, вам нужно использовать int (&)().


Почему я не могу добавить && к Ret (Args...) &

Кажется, что квалифицированные (ref или cv-квалифицированные) типы функций могут использоваться только в очень специфических местах:

* * тысяча двадцать-один [dcl.typedef] / 6 * ** 1023 тысячи двадцать-два * Тип функции с cv-qualifier-seq или ref-qualifier (включая тип с именем typedef-name ([dcl.typedef], [temp.param])) должен отображаются только как: - тип функции для нестатического члена функция, - тип функции, к которой относится указатель на член, - тип функции верхнего уровня объявления функции typedef или Псевдоним декларация - идентификатор типа в аргументе по умолчанию для параметра типа или - идентификатор типа аргумента шаблона для параметра типа ([temp.arg.type]).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...