带有decltype的函数的指针向量

时间:2018-09-10 20:21:00

标签: c++ function pointers vector decltype

我正在尝试建立指向函数的指针向量。这是我的功能:

int func(const unsigned int &myInt)
{
    std::cout << __func__ << "\nMy numb is " << myInt << std::endl;
    return myInt;
}

类型为int (const unsigned int&)的简单函数。当我尝试使用int (*)(const unsigned int&)(与*一起使用,因为我们需要一个指针向量)类型时,一切都很好,并且编译时没有错误。

int func(const unsigned int &myInt)
{
    std::cout << __func__ << "\nMy numb is " << myInt << std::endl; // we print the name of the function and the integer provided
    return myInt;
}


int main()
{
    std::vector<int (*)(const unsigned int&)> myVector;
    myVector.push_back(func); // the first element is the function
    myVector[0](999);         // we call it
    return 0;
}

输出符合预期:

func
My numb is 999

我被告知decltype*运算符一起使用时会返回这种类型,但是当我尝试对其进行编译时,它将失败:

int func(const unsigned int &myInt)
{
    std::cout << __func__ << "\nMy numb is " << myInt << std::endl;
    return myInt;
}


int main()
{
    std::vector<decltype(*func)> myVector;
    myVector.push_back(func);
    myVector[0](999);
    return 0;
}

为了确保decltype(*func)是我想要的类型,我编写了另一个函数,该函数将这个作为参数,运行并返回它:

decltype(*func) controlFunc(const unsigned int &myInt, const decltype(*func) &myFunc)
{
    myFunc(myInt);
    return myFunc;
}

这个可以编译并运行得很好。

controlFunc(999, func)(999); // we run the function returned
// output:
// func
// My numb is 999
// func
// My numb is 999

decltype中的vector<decltype(*func)>有什么特别之处?我知道在不使用*的情况下,decltype返回一个函数类型,而不是函数指针,因此在我们的向量中没有用。如何使用decltype创建向量?

我收到的错误消息是:

In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ext\alloc_traits.h:36:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:52,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\locale_classes.h:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\ios_base.h:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ios:42,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream:39,
                 from C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:1:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\alloc_traits.h: In instantiation of 'struct std::allocator_traits<std::allocator<int (&)(const unsigned int&)> >':
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ext\alloc_traits.h:50:10:   required from 'struct __gnu_cxx::__alloc_traits<std::allocator<int (&)(const unsigned int&)> >'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:75:28:   required from 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:214:11:   required from 'class std::vector<int (&)(const unsigned int&)>'
C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:12:31:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\alloc_traits.h:372:27: error: forming pointer to reference type 'int (&)(const unsigned int&)'
       using pointer = _Tp*;
                           ^
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\alloc_traits.h:375:39: error: forming pointer to reference type 'int (&)(const unsigned int&)'
       using const_pointer = const _Tp*;
                                       ^
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\basic_string.h:40:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:52,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\locale_classes.h:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\ios_base.h:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ios:42,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream:39,
                 from C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:1:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ext\alloc_traits.h: In instantiation of 'struct __gnu_cxx::__alloc_traits<std::allocator<int (&)(const unsigned int&)> >':
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:75:28:   required from 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:214:11:   required from 'class std::vector<int (&)(const unsigned int&)>'
C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:12:31:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ext\alloc_traits.h:66:23: error: no members matching '__gnu_cxx::__alloc_traits<std::allocator<int (&)(const unsigned int&)> >::_Base_type {aka std::allocator_traits<std::allocator<int (&)(const unsigned int&)> >}::allocate' in '__gnu_cxx::__alloc_traits<std::allocator<int (&)(const unsigned int&)> >::_Base_type {aka struct std::allocator_traits<std::allocator<int (&)(const unsigned int&)> >}'
     using _Base_type::allocate;
                       ^~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ext\alloc_traits.h:67:23: error: no members matching '__gnu_cxx::__alloc_traits<std::allocator<int (&)(const unsigned int&)> >::_Base_type {aka std::allocator_traits<std::allocator<int (&)(const unsigned int&)> >}::deallocate' in '__gnu_cxx::__alloc_traits<std::allocator<int (&)(const unsigned int&)> >::_Base_type {aka struct std::allocator_traits<std::allocator<int (&)(const unsigned int&)> >}'
     using _Base_type::deallocate;
                       ^~~~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\mingw32\bits\c++allocator.h:33:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\allocator.h:46,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\locale_classes.h:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\ios_base.h:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ios:42,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream:39,
                 from C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:1:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ext\new_allocator.h: In instantiation of 'class __gnu_cxx::new_allocator<int (&)(const unsigned int&)>':
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\allocator.h:108:11:   required from 'class std::allocator<int (&)(const unsigned int&)>'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:79:14:   required from 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >::_Vector_impl'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:164:20:   required from 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:214:11:   required from 'class std::vector<int (&)(const unsigned int&)>'
C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:12:31:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ext\new_allocator.h:63:26: error: forming pointer to reference type 'int (&)(const unsigned int&)'
       typedef _Tp*       pointer;
                          ^~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ext\new_allocator.h:64:26: error: forming pointer to reference type 'int (&)(const unsigned int&)'
       typedef const _Tp* const_pointer;
                          ^~~~~~~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\string:41:0,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\locale_classes.h:40,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\ios_base.h:41,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ios:42,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\ostream:38,
                 from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\iostream:39,
                 from C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:1:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\allocator.h: In instantiation of 'class std::allocator<int (&)(const unsigned int&)>':
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:79:14:   required from 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >::_Vector_impl'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:164:20:   required from 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:214:11:   required from 'class std::vector<int (&)(const unsigned int&)>'
C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:12:31:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\allocator.h:113:26: error: forming pointer to reference type 'int (&)(const unsigned int&)'
       typedef _Tp*       pointer;
                          ^~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\allocator.h:114:26: error: forming pointer to reference type 'int (&)(const unsigned int&)'
       typedef const _Tp* const_pointer;
                          ^~~~~~~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\vector:64:0,
                 from C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:2:
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h: In instantiation of 'class std::vector<int (&)(const unsigned int&)>':
C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:12:31:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:243:20: error: no members matching 'std::vector<int (&)(const unsigned int&)>::_Base {aka std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >}::_M_allocate' in 'std::vector<int (&)(const unsigned int&)>::_Base {aka struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >}'
       using _Base::_M_allocate;
                    ^~~~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:244:20: error: no members matching 'std::vector<int (&)(const unsigned int&)>::_Base {aka std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >}::_M_deallocate' in 'std::vector<int (&)(const unsigned int&)>::_Base {aka struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >}'
       using _Base::_M_deallocate;
                    ^~~~~~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:891:7: error: forming pointer to reference type 'int (&)(const unsigned int&)'
       data() _GLIBCXX_NOEXCEPT
       ^~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:899:7: error: forming pointer to reference type 'int (&)(const unsigned int&)'
       data() const _GLIBCXX_NOEXCEPT
       ^~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:932:7: error: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = int (&)(const unsigned int&); _Alloc = std::allocator<int (&)(const unsigned int&)>; std::vector<_Tp, _Alloc>::value_type = int (&)(const unsigned int&)]' cannot be overloaded
       push_back(value_type&& __x)
       ^~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:914:7: error: with 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int (&)(const unsigned int&); _Alloc = std::allocator<int (&)(const unsigned int&)>; std::vector<_Tp, _Alloc>::value_type = int (&)(const unsigned int&)]'
       push_back(const value_type& __x)
       ^~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h: In instantiation of 'std::vector<_Tp, _Alloc>::~vector() [with _Tp = int (&)(const unsigned int&); _Alloc = std::allocator<int (&)(const unsigned int&)>]':
C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:12:31:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:426:37: error: 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >::_Vector_impl' has no member named '_M_start'; did you mean '_M_swap_data'?
       { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
                       ~~~~~~~~~~~~~~^~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:426:61: error: 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >::_Vector_impl' has no member named '_M_finish'
       { std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
                                               ~~~~~~~~~~~~~~^~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::~_Vector_base() [with _Tp = int (&)(const unsigned int&); _Alloc = std::allocator<int (&)(const unsigned int&)>]':
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:259:15:   required from 'std::vector<_Tp, _Alloc>::vector() [with _Tp = int (&)(const unsigned int&); _Alloc = std::allocator<int (&)(const unsigned int&)>]'
C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:12:31:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:160:37: error: 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >::_Vector_impl' has no member named '_M_start'; did you mean '_M_swap_data'?
       { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
                       ~~~~~~~~~~~~~~^~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:160:61: error: 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >::_Vector_impl' has no member named '_M_end_of_storage'
       { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
                                               ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:161:25: error: 'struct std::_Vector_base<int (&)(const unsigned int&), std::allocator<int (&)(const unsigned int&)> >::_Vector_impl' has no member named '_M_start'; did you mean '_M_swap_data'?
         - this->_M_impl._M_start); }
           ~~~~~~~~~~~~~~^~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:160:22: error: '_M_deallocate' was not declared in this scope
       { _M_deallocate(this->_M_impl._M_start, this->_M_impl._M_end_of_storage
         ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         - this->_M_impl._M_start); }
         ~~~~~~~~~~~~~~~~~~~~~~~~~
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h: In instantiation of 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_Vector_impl() [with _Tp = int (&)(const unsigned int&); _Alloc = std::allocator<int (&)(const unsigned int&)>]':
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:125:17:   required from 'std::_Vector_base<_Tp, _Alloc>::_Vector_base() [with _Tp = int (&)(const unsigned int&); _Alloc = std::allocator<int (&)(const unsigned int&)>]'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:259:15:   required from 'std::vector<_Tp, _Alloc>::vector() [with _Tp = int (&)(const unsigned int&); _Alloc = std::allocator<int (&)(const unsigned int&)>]'
C:\Users\User\Desktop\C++_Primer\Exercises\Test\test.cpp:12:31:   required from here
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:87:65: error: using invalid field 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_start'
  : _Tp_alloc_type(), _M_start(), _M_finish(), _M_end_of_storage()
                                                                 ^
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:87:65: error: using invalid field 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_finish'
c:\mingw\lib\gcc\mingw32\6.3.0\include\c++\bits\stl_vector.h:87:65: error: using invalid field 'std::_Vector_base<_Tp, _Alloc>::_Vector_impl::_M_end_of_storage'

2 个答案:

答案 0 :(得分:1)

问题是decltype(*func)产生了引用类型,因此:

static_assert(std::is_same_v<decltype(*func), int(&)(const  unsigned int&)>);

编译,但是在您的情况下std::vector保留类型T的指针T是引用类型,指向引用的指针是非法的。要解决此问题,您可以在decltype(*func)时将decltype(&func)替换为T=int(*)(const unsigned int&),也可以在T=std::function<int(const unsigned int&)>之外创建一个向量

答案 1 :(得分:1)

SharedModule(和decltype(*fun))计算为引用类型。创建引用类型的decltype(fun)无效。

来自https://en.cppreference.com/w/cpp/container/vector

  

T必须满足CopyAssignableCopyConstructible的要求。

引用类型不符合那些要求。

我建议使用std::vector

std::vector<std::function>