C++ 为什么C++;允许类型没有std::map的默认构造函数,而不是std::pair?
检查以下C++ 为什么C++;允许类型没有std::map的默认构造函数,而不是std::pair?,c++,C++,检查以下C++代码: #include <string> #include <map> class A { public: A (int a) {}; }; int main() { std::map<std::string, A> p; return 0; } 编译器将抱怨: $ clang++ test.cpp In file included from test.cpp:1: In file included from /
C++
代码:
#include <string>
#include <map>
class A
{
public:
A (int a) {};
};
int main()
{
std::map<std::string, A> p;
return 0;
}
编译器将抱怨:
$ clang++ test.cpp
In file included from test.cpp:1:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/string:40:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/char_traits.h:39:
In file included from /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/stl_algobase.h:64:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../include/c++/7.3.0/bits/stl_pair.h:219:18: error: no matching
constructor for initialization of 'A'
: first(), second() { }
^
test.cpp:13:31: note: in instantiation of member function 'std::pair<std::__cxx11::basic_string<char>, A>::pair'
requested here
std::pair<std::string, A> p;
^
test.cpp:7:5: note: candidate constructor not viable: requires single argument 'a', but no arguments were provided
A (int a) {};
^
test.cpp:4:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were
provided
class A
^
1 error generated.
$clang++test.cpp
在test.cpp中包含的文件中:1:
在/usr/bin/./lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../../../../include/c++/7.3.0/string:40中包含的文件中:
在/usr/bin/./lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../../../../../include/c++/7.3.0/bits/char\u traits中包含的文件中。h:39:
在/usr/bin/./lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../../../../include/c++/7.3.0/bits/stl_algobase.h:64中包含的文件中:
/usr/bin/./lib64/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../../../include/c++/7.3.0/bits/stl_pair.h:219:18:错误:不匹配
用于初始化“A”的构造函数
:first(),second(){}
^
test.cpp:13:31:注意:在成员函数'std::pair::pair'的实例化中
在此请求
std::对p;
^
test.cpp:7:5:注意:候选构造函数不可行:需要单个参数“a”,但未提供任何参数
A(int A){};
^
test.cpp:4:7:注意:候选构造函数(隐式副本构造函数)不可行:需要1个参数,但0不可用
假如
甲级
^
生成1个错误。
为什么
C++
allow类型没有std::map
的默认构造函数,而没有std::pair
std::map
在构造时为空,这意味着它还不需要构造A
。另一方面,std::pair必须这样做才能完成初始化
因为两者都是类模板,所以只有您使用的成员函数才被实际实例化。如果要查看预期的错误,需要让映射尝试构造默认的a
,例如:
int main()
{
std::map<std::string, A> p;
p[""];
}
intmain()
{
std::map p;
p[“”];
}
差异是由于A
不是默认可构造的。pair case会立即检测到这一点,因为它将尝试默认构造一个实例
最终,map案例将产生相同的错误。如果您编写p[“Hello”],您可以看到这一点代码>:
int main()
{
std::map<std::string, A> p;
p["Hello"]; // oops - default constructor required here for `A`
}
intmain()
{
std::map p;
p[“Hello”];//oops-此处需要的默认构造函数用于`A`
}
因为std::map
的默认构造函数不调用其元素的默认构造函数,而std::pair
doesIIRC,所以std::map
中唯一需要元素默认构造函数的部分是[]
操作符,您可以使用std::map
,而不必使用它。“映射案例最终将产生相同的错误。”好吧,不一定,最终。如果不使用std::map::operator[]
(例如,使用std::map::insert
进行元素插入,使用std::map::find
进行查找),可以使用std::map
存储没有默认构造函数的类型的值。
int main()
{
std::map<std::string, A> p;
p["Hello"]; // oops - default constructor required here for `A`
}