C++ 那些被遗漏的细节4 std::piecewise_construct_t

本文详细介绍了C++中std::pair的分段构造函数,特别是std::piecewise_construct_t的作用。这个构造函数允许通过两个tuple参数分别初始化pair的key和value,尤其适用于构造复杂类型的pair。示例代码展示了如何利用std::piecewise_construct_t和tuple来创建含有自定义类型Foo的pair,展示了不同的构造方式导致的不同行为。
简述
  • 在以前的文章C++ std::pair 有对std::pair做简单介绍。
  • 需要注意的是std::pair有一个特别的构造函数,第一个参数类型为std::piecewise_construct_t, 实际就是一个空结构体类型,用作标识。
  • std::piecewise_construct_t作用就是其字面意思:分段构造。具体来说,因pair的key和value均可以是构造函数复杂类型,因而pair的初始化相对复杂,通过带有std::piecewise_construct_t类型参数,后跟两个tuple类型参数的构造函数,用第一个tuple类型的元素来构造pair的key, 用第二个tuple类型的元素来构造pair的value,从而实现pair的初始化。
  • forward_as_tuple经常与std::piecewise_construct_t配合使用
  • pair构造函数
// 头文件<utility>
pair(); (until C++11)(1)
constexpr pair(); (since C++11)(1)
(conditionally explicit)

pair( const T1& x, const T2& y );(until C++11)(2)
pair( const T1& x, const T2& y );(since C++11)(until C++14)(conditionally explicit)(2)
constexpr pair( const T1& x, const T2& y );(since C++14)(conditionally explicit)(2)
	
template< class U1, class U2 >
pair( U1&& x, U2&& y );(since C++11)(until C++14)(conditionally explicit)(3)
template< class U1, class U2 >
constexpr pair( U1&& x, U2&& y );(since C++14)(until C++23)(conditionally explicit)(3)
template< class U1 = T1, class U2 = T2 >
constexpr pair( U1&& x, U2&& y );(since C++23)(conditionally explicit)(3)

template< class U1, class U2 >
pair( const pair<U1, U2>& p );(until C++11)(4)	
template< class U1, class U2 >
pair( const pair<U1, U2>& p );(since C++11)(conditionally explicit)(4)	

template< class U1, class U2 >
pair( pair<U1, U2>&& p );(since C++11)(until C++14)(conditionally explicit)(5)	
template< class U1, class U2 >
constexpr pair( pair<U1, U2>&& p );(since C++14)(conditionally explicit)(5)	

template< class... Args1, class... Args2 >
pair( std::piecewise_construct_t,
      std::tuple<Args1...> first_args,
      std::tuple<Args2...> second_args );(since C++11)(until C++20)(6)	// 注意
template< class... Args1, class... Args2 >
constexpr pair( std::piecewise_construct_t,
                std::tuple<Args1...> first_args,
                std::tuple<Args2...> second_args );(since C++20)(6)	 // 注意
pair( const pair& p ) = default;(7)	
pair( pair&& p ) = default;(since C++11)(8)	
举例
#include <iostream>
#include <utility> // pair
#include <tuple>

struct Foo
{
    Foo(std::tuple<int, float>)
    {
        std::cout << "Constructed a Foo from a tuple\n";
    }
    Foo(int, float)
    {
        std::cout << "Constructed a Foo from an int and a float\n";
    }
};

int main()
{
    std::tuple<int, float> t(1, 3.14);
    std::pair<Foo, Foo> p1(t, t);
    std::pair<Foo, Foo> p2(std::piecewise_construct, t, t);
    return 0;
}
  • 结果
lee@leedeMacBook-Pro piecewise_construct % ./main
Constructed a Foo from a tuple
Constructed a Foo from a tuple
Constructed a Foo from an int and a float
Constructed a Foo from an int and a float
参考
In file included from D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/hashtable_policy.h:34, from D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/hashtable.h:35, from D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/unordered_map.h:33, from D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/unordered_map:41, from D:/obj/i wanna own world to me/i wanna own world to me.cpp:9: D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/tuple: In instantiation of 'constexpr std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&}; long long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long long unsigned int ..._Indexes2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = SDL_GL_Image]': D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/tuple:2877:63: required from 'constexpr std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&}; _Args2 = {}; _T1 = const std::__cxx11::basic_string<char>; _T2 = SDL_GL_Image]' 2877 | typename _Build_index_tuple<sizeof...(_Args2)>::__type()) | ^ D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/stl_construct.h:97:14: required from 'constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...) [with _Tp = pair<const __cxx11::basic_string<char>, SDL_GL_Image>; _Args = {const piecewise_construct_t&, tuple<__cxx11::basic_string<char, char_traits<char>, allocator<char> >&&>, tuple<>}; decltype (::new(void*(0)) _Tp) = pair<const __cxx11::basic_string<char>, SDL_GL_Image>*]' 97 | { return ::new((void*)__location) _Tp(std::forward<_Args>(__args)...); } | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/alloc_traits.h:536:21: required from 'static constexpr void std::allocator_traits<std::allocator<_Up> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image>; _Args = {const std::piecewise_construct_t&, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Tp = std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image>, true>; allocator_type = std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image>, true> >]' 536 | std::construct_at(__p, std::forward<_Args>(__args)...); | ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/hashtable_policy.h:2024:36: required from 'std::__detail::_Hashtable_alloc<_NodeAlloc>::__node_type* std::__detail::_Hashtable_alloc<_NodeAlloc>::_M_allocate_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _NodeAlloc = std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image>, true> >; __node_ptr = std::allocator<std::__detail::_Hash_node<std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image>, true> >::value_type*]' 2024 | __node_alloc_traits::construct(__alloc, __n->_M_valptr(), | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~ 2025 | std::forward<_Args>(__args)...); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/hashtable.h:312:35: required from 'std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::_Scoped_node::_Scoped_node(std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::__hashtable_alloc*, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&&>, std::tuple<>}; _Key = std::__cxx11::basic_string<char>; _Value = std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image>; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image> >; _ExtractKey = std::__detail::_Select1st; _Equal = std::equal_to<std::__cxx11::basic_string<char> >; _Hash = std::hash<std::__cxx11::basic_string<char> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; std::_Hashtable<_Key, _Value, _Alloc, _ExtractKey, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits>::__hashtable_alloc = std::_Hashtable<std::__cxx11::basic_string<char>, std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image>, std::allocator<std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image> >, std::__detail::_Select1st, std::equal_to<std::__cxx11::basic_string<char> >, std::hash<std::__cxx11::basic_string<char> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy, std::__detail::_Hashtable_traits<true, false, true> >::__hashtable_alloc]' 312 | _M_node(__h->_M_allocate_node(std::forward<_Args>(__args)...)) | ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/hashtable_policy.h:870:42: required from 'std::__detail::_Map_base<_Key, std::pair<const _Key, _Val>, _Alloc, std::__detail::_Select1st, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::mapped_type& std::__detail::_Map_base<_Key, std::pair<const _Key, _Val>, _Alloc, std::__detail::_Select1st, _Equal, _Hash, _RangeHash, _Unused, _RehashPolicy, _Traits, true>::operator[](key_type&&) [with _Key = std::__cxx11::basic_string<char>; _Val = SDL_GL_Image; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image> >; _Equal = std::equal_to<std::__cxx11::basic_string<char> >; _Hash = std::hash<std::__cxx11::basic_string<char> >; _RangeHash = std::__detail::_Mod_range_hashing; _Unused = std::__detail::_Default_ranged_hash; _RehashPolicy = std::__detail::_Prime_rehash_policy; _Traits = std::__detail::_Hashtable_traits<true, false, true>; mapped_type = SDL_GL_Image; key_type = std::__cxx11::basic_string<char>]' 870 | typename __hashtable::_Scoped_node __node { | ^~~~~~ D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/bits/unordered_map.h:992:20: required from 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::mapped_type& std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::operator[](key_type&&) [with _Key = std::__cxx11::basic_string<char>; _Tp = SDL_GL_Image; _Hash = std::hash<std::__cxx11::basic_string<char> >; _Pred = std::equal_to<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, SDL_GL_Image> >; mapped_type = SDL_GL_Image; key_type = std::__cxx11::basic_string<char>]' 992 | { return _M_h[std::move(__k)]; } | ~~~~^ D:/obj/i wanna own world to me/i wanna own world to me.cpp:164:57: required from here 164 | glBindTexture(GL_TEXTURE_2D, images["sprBrownBlock"s].textureID); | ^ D:/SelfUsing/Language/CLion 2025.1.3/mingw/lib/gcc/x86_64-w64-mingw32/14.2.0/include/c++/tuple:2888:9: error: no matching function for call to 'SDL_GL_Image::SDL_GL_Image()' 2888 | second(std::forward<_Args2>(std::get<_Indexes2>(__tuple2))...) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ D:/obj/i wanna own world to me/i wanna own world to me.cpp:43:14: note: candidate: 'SDL_GL_Image::SDL_GL_Image(const char*)' 43 | explicit SDL_GL_Image(const char* path) { | ^~~~~~~~~~~~ D:/obj/i wanna own world to me/i wanna own world to me.cpp:43:14: note: candidate expects 1 argument, 0 provided D:/obj/i wanna own world to me/i wanna own world to me.cpp:39:7: note: candidate: 'constexpr SDL_GL_Image::SDL_GL_Image(const SDL_GL_Image&)' 39 | union SDL_GL_Image { | ^~~~~~~~~~~~ D:/obj/i wanna own world to me/i wanna own world to me.cpp:39:7: note: candidate expects 1 argument, 0 provided ninja: build stopped: subcommand failed.
08-05
C:\Users\admin\Documents\未命名4.cpp In function 'void handleClient(SOCKET)': 116 21 C:\Users\admin\Documents\未命名4.cpp [Error] no match for 'operator=' (operand types are 'std::map<long long unsigned int, Player>::mapped_type {aka Player}' and '<brace-enclosed initializer list>') 116 21 C:\Users\admin\Documents\未命名4.cpp [Note] candidates are: 35 8 C:\Users\admin\Documents\未命名4.cpp [Note] Player& Player::operator=(const Player&) 35 8 C:\Users\admin\Documents\未命名4.cpp [Note] no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'const Player&' 35 8 C:\Users\admin\Documents\未命名4.cpp [Note] Player& Player::operator=(Player&&) 35 8 C:\Users\admin\Documents\未命名4.cpp [Note] no known conversion for argument 1 from '<brace-enclosed initializer list>' to 'Player&&' 139 24 C:\Users\admin\Documents\未命名4.cpp [Error] expected unqualified-id before '[' token 139 24 C:\Users\admin\Documents\未命名4.cpp [Error] expected ';' before '[' token 139 25 C:\Users\admin\Documents\未命名4.cpp [Error] 'id' was not declared in this scope 139 29 C:\Users\admin\Documents\未命名4.cpp [Error] 'room' was not declared in this scope C:\Users\admin\Documents\未命名4.cpp In lambda function: 139 35 C:\Users\admin\Documents\未命名4.cpp [Error] expected '{' before ':' token C:\Users\admin\Documents\未命名4.cpp In function 'void handleClient(SOCKET)': 139 35 C:\Users\admin\Documents\未命名4.cpp [Error] expected ';' before ':' token 139 35 C:\Users\admin\Documents\未命名4.cpp [Error] expected primary-expression before ':' token 139 35 C:\Users\admin\Documents\未命名4.cpp [Error] expected ')' before ':' token 139 35 C:\Users\admin\Documents\未命名4.cpp [Error] expected primary-expression before ':' token 55 0 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\functional In file included from C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/functional 39 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\thread from C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/thread 4 C:\Users\admin\Documents\未命名4.cpp from C:\Users\admin\Documents\未命名4.cpp C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\tuple In instantiation of 'std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {const int&}; long long unsigned int ..._Indexes1 = {0ull}; _Args2 = {}; long long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = Room]': 1091 63 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\tuple required from 'std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const int&}; _Args2 = {}; _T1 = const int; _T2 = Room]' 120 4 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\ext\new_allocator.h required from 'void __gnu_cxx::new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, Room>; _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Tp = std::_Rb_tree_node<std::pair<const int, Room> >]' 253 4 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\alloc_traits.h required from 'static std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> std::allocator_traits<_Alloc>::_S_construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::pair<const int, Room>; _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Alloc = std::allocator<std::_Rb_tree_node<std::pair<const int, Room> > >; std::_Require<typename std::allocator_traits<_Alloc>::__construct_helper<_Tp, _Args>::type> = void]' 399 57 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\alloc_traits.h required from 'static decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) std::allocator_traits<_Alloc>::construct(_Alloc&, _Tp*, _Args&& ...) [with _Tp = std::pair<const int, Room>; _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Alloc = std::allocator<std::_Rb_tree_node<std::pair<const int, Room> > >; decltype (_S_construct(__a, __p, (forward<_Args>)(std::allocator_traits::construct::__args)...)) = <type error>]' 423 42 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\stl_tree.h required from 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Room>; _KeyOfValue = std::_Select1st<std::pair<const int, Room> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Room> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, Room> >*]' 1790 64 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\stl_tree.h required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, Room>; _KeyOfValue = std::_Select1st<std::pair<const int, Room> >; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Room> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, Room> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const int, Room> >]' 500 8 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\stl_map.h required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = int; _Tp = Room; _Compare = std::less<int>; _Alloc = std::allocator<std::pair<const int, Room> >; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = Room; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]' 156 33 C:\Users\admin\Documents\未命名4.cpp required from here 1102 70 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\tuple [Error] no matching function for call to 'Room::Room()' 1102 70 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\tuple [Note] candidates are: 47 5 C:\Users\admin\Documents\未命名4.cpp [Note] Room::Room(int) 47 5 C:\Users\admin\Documents\未命名4.cpp [Note] candidate expects 1 argument, 0 provided 41 8 C:\Users\admin\Documents\未命名4.cpp [Note] Room::Room(const Room&) 41 8 C:\Users\admin\Documents\未命名4.cpp [Note] candidate expects 1 argument, 0 provided 41 8 C:\Users\admin\Documents\未命名4.cpp [Note] Room::Room(Room&&) 41 8 C:\Users\admin\Documents\未命名4.cpp [Note] candidate expects 1 argument, 0 provided
09-14
In file included from default.cpp:1: In file included from SYSROOT/usr/include/bits/stdc++.h:79: SYSROOT/usr/include/c++/v1/map:521:17: error: no matching function for call to object of type 'const compare' {return static_cast<const _Compare&>(*this)(__x, __y.__get_value().first);} ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SYSROOT/usr/include/c++/v1/__tree:1975:17: note: in instantiation of member function 'std::__map_value_compare<int, std::__value_type<int, std::pair<int, int>>, compare, true>::operator()' requested here if (value_comp()(__v, __nd->__value_)) ^ SYSROOT/usr/include/c++/v1/__tree:2091:36: note: in instantiation of function template specialization 'std::__tree<std::__value_type<int, std::pair<int, int>>, std::__map_value_compare<int, std::__value_type<int, std::pair<int, int>>, compare, true>, std::allocator<std::__value_type<int, std::pair<int, int>>>>::__find_equal<int>' requested here __node_base_pointer& __child = __find_equal(__parent, __k); ^ SYSROOT/usr/include/c++/v1/map:1521:20: note: in instantiation of function template specialization 'std::__tree<std::__value_type<int, std::pair<int, int>>, std::__map_value_compare<int, std::__value_type<int, std::pair<int, int>>, compare, true>, std::allocator<std::__value_type<int, std::pair<int, int>>>>::__emplace_unique_key_args<int, const std::piecewise_construct_t &, std::tuple<const int &>, std::tuple<>>' requested here return __tree_.__emplace_unique_key_args(__k, ^ default.cpp:19:14: note: in instantiation of member function 'std::map<int, std::pair<int, int>, compare>::operator[]' requested here m[a].first++; ^ default.cpp:5:10: note: candidate function not viable: no known conversion from 'const int' to 'pair<int, int> &' for 1st argument bool operator()(pair<int,int>&p1,pair<int,int>&p2)const{ ^ In file included from default.cpp:1: In file included from SYSROOT/usr/include/bits/stdc++.h:79: SYSROOT/usr/include/c++/v1/map:518:17: error: no matching function for call to object of type 'const compare' {return static_cast<const _Compare&>(*this)(__x.__get_value().first, __y);} ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SYSROOT/usr/include/c++/v1/__tree:1985:22: note: in instantiation of member function 'std::__map_value_compare<int, std::__value_type<int, std::pair<int, int>>, compare, true>::operator()' requested here else if (value_comp()(__nd->__value_, __v)) ^ SYSROOT/usr/include/c++/v1/__tree:2091:36: note: in instantiation of function template specialization 'std::__tree<std::__value_type<int, std::pair<int, int>>, std::__map_value_compare<int, std::__value_type<int, std::pair<int, int>>, compare, true>, std::allocator<std::__value_type<int, std::pair<int, int>>>>::__find_equal<int>' requested here __node_base_pointer& __child = __find_equal(__parent, __k); ^ SYSROOT/usr/include/c++/v1/map:1521:20: note: in instantiation of function template specialization 'std::__tree<std::__value_type<int, std::pair<int, int>>, std::__map_value_compare<int, std::__value_type<int, std::pair<int, int>>, compare, true>, std::allocator<std::__value_type<int, std::pair<int, int>>>>::__emplace_unique_key_args<int, const std::piecewise_construct_t &, std::tuple<const int &>, std::tuple<>>' requested here return __tree_.__emplace_unique_key_args(__k, ^ default.cpp:19:14: note: in instantiation of member function 'std::map<int, std::pair<int, int>, compare>::operator[]' requested here m[a].first++; ^ default.cpp:5:10: note: candidate function not viable: no known conversion from 'const int' to 'pair<int, int> &' for 1st argument bool operator()(pair<int,int>&p1,pair<int,int>&p2)const{ ^ 2 errors generated. 以上为报错,以下为我的代码,请告诉我错误原因和如何修改
最新发布
10-29
#/home/adam/Android/sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-linux-android28 --sysroot=/home/adam/Android/sdk/ndk/26.1.10909125/toolchains/llvm/prebuilt/linux-x86_64/sysroot -o syshandle syshandle.cpp g++ -o syshandle syshandle.cpp syshandle.cpp: In function ‘auto syshandleInit()’: syshandle.cpp:330:83: error: no matching function for call to ‘myread::myread(<brace-enclosed initializer list>)’ 330 | auto mm=new myread{{{"Seccomp:$#",setZero},{"Seccomp_filters:$#",setZero}}}); | ^ syshandle.cpp:281:9: note: candidate: ‘myread::myread(const stateMM&)’ 281 | myread(const stateMM& s):syshandle(),sm(s){}; | ^~~~~~ syshandle.cpp:281:31: note: no known conversion for argument 1 from ‘<brace-enclosed initializer list>’ to ‘const stateMM&’ 281 | myread(const stateMM& s):syshandle(),sm(s){}; | ~~~~~~~~~~~~~~~^ In file included from /usr/include/c++/13/bits/memory_resource.h:47, from /usr/include/c++/13/list:73, from syshandle.cpp:1: /usr/include/c++/13/tuple: In instantiation of ‘std::pair<_T1, _T2>::pair(std::tuple<_Args1 ...>&, std::tuple<_Args2 ...>&, std::_Index_tuple<_Indexes1 ...>, std::_Index_tuple<_Indexes2 ...>) [with _Args1 = {const int&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {}; long unsigned int ..._Indexes2 = {}; _T1 = const int; _T2 = stateMM]’: /usr/include/c++/13/tuple:2257:63: required from ‘std::pair<_T1, _T2>::pair(std::piecewise_construct_t, std::tuple<_Args1 ...>, std::tuple<_Args2 ...>) [with _Args1 = {const int&}; _Args2 = {}; _T1 = const int; _T2 = stateMM]’ /usr/include/c++/13/bits/new_allocator.h:191:4: required from ‘void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::pair<const int, stateMM>; _Args = {const std::piecewise_construct_t&, std::tuple<const int&>, std::tuple<>};
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值