代码之家  ›  专栏  ›  技术社区  ›  Aykhan Hagverdili

指向decltype函数的指针向量

  •  1
  • Aykhan Hagverdili  · 技术社区  · 6 年前

    我试图得到一个指向函数的指针向量。我的职责是:

    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
    

    有什么特别的吗 脱模 在里面 vector<decltype(*func)> ? 我知道不用的时候 * , 脱模 返回函数类型,而不是指向函数的指针,因此在向量中没有用处。如何使用 脱模

    我收到的错误消息是:

    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 回复  |  直到 6 年前
        1
  •  2
  •   Aykhan Hagverdili    4 年前

    问题是 decltype(*func) 生成引用类型,以便:

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

    编译,但是 std::vector 保留类型的指针 T . 对你来说 T型 是引用类型,指向引用的指针非法。要修复它,可以替换 通过 decltype(&func) 什么时候 T=int(*)(const unsigned int&) T=std::function<int(const unsigned int&)>

        2
  •  2
  •   R Sahu    6 年前

    decltype(*fun) (和 decltype(fun) )计算为引用类型。创建 std::vector 引用类型无效。

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

    CopyAssignable CopyConstructible .

    引用类型不满足这些要求。

    std::vector<std::function> .

    #include <iostream>
    #include <vector>
    #include <functional>
    
    int func(const unsigned int &myInt)
    {
        std::cout << __func__ << "\nMy numb is " << myInt << std::endl;
        return myInt;
    }
    
    
    int main()
    {
       using function_t = std::function<int(const unsigned int&)>;
    
       std::vector<function_t> myVector;
       myVector.push_back(func);
       myVector[0](999);
       return 0;
    }