简体   繁体   English

为什么C ++允许类型为std :: map而没有默认构造函数而不是std :: pair?

[英]Why does C++ allow type has no-default constructor for std::map while not std::pair?

Check the following C++ code: 检查以下C++代码:

#include <string>
#include <map>

class A
{
public:
    A (int a) {};
};

int main()
{

    std::map<std::string, A> p;
    return 0;
}

The compilation is successful. 编译成功。 While change std::map to std::pair : std::map更改为std::pair

#include <string>
#include <utility>

class A
{
public:
    A (int a) {};
};

int main()
{

    std::pair<std::string, A> p;
    return 0;
}

The compiler will complain: 编译器会抱怨:

$ 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.

Why does C++ allow type has no-default constructor for std::map while not std::pair ? 为什么C++允许类型为std::map而没有默认构造函数而不是std::pair

std::map is empty when constructed, which means that it does not need to construct an A just yet. 构造时std::map为空,这意味着它不需要构造A The std::pair , on the other hand, has to do it in order to complete its initialization. 另一方面, std::pair必须这样做才能完成初始化。

Since both are class templates, only the member functions you use are actually instantiated. 由于两者都是类模板,因此实际上只实例化了您使用的成员函数。 If you want to see the error you expect, you need to have the map try to construct a default A , for example: 如果要查看预期的错误,则需要让映射尝试构造默认A ,例如:

int main()
{
    std::map<std::string, A> p;

    p[""];
}

The difference is due to A not being default-constructible. 差异是由于A不是默认构造的。 The pair case picks this up immediately as it will attempt to default-construct an A instance. 对案例立即选择它,因为它将尝试默认构造A实例。

Eventually the map case will yield the same error. 最终地图案例将产生相同的错误。 You can see this if you write p["Hello"]; 如果你写p["Hello"];你可以看到这个p["Hello"]; :

int main()
{
    std::map<std::string, A> p;
    p["Hello"]; // oops - default constructor required here for `A`
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么`std :: pair`允许使用用户定义的删除move构造函数从类类型的右值进行初始化? - Why does `std::pair` allow to initialize from an rvalue of class type with a user-defined deleted move constructor? 在C ++中对std :: pair和std :: map进行子类化 - Subclassing std::pair and std::map in C++ 是std :: map <std::set<long> ,double&gt; AND std:map &lt;std :: pair <long, long> ,double&gt; C ++中的有效数据类型? - Is std::map<std::set<long>, double> AND std:map< std::pair<long, long>, double> a valid data type in C++? 为什么std :: map :: const_iterator在std :: for_each期间调用std :: pair构造函数,但是一个简单的for循环却没有? - Why does std::map::const_iterator call the std::pair constructor during a std::for_each, but a simple for loop does not? C ++ STL map,std :: pair作为键 - C++ STL map , std::pair as the key 为什么std :: map需要一对? - why does std::map take a pair? C ++ std :: map和std :: pair <int, int> 作为关键 - C++ std::map and std::pair<int, int> as Key 在SOLARIS上使用C ++ Pair初始化C ++ std映射时出错 - Error while init of C++ std map with C++ Pair ON SOLARIS 为什么std :: map接受std :: pair作为键,但std :: unordered_map不接受? - Why does std::map accept a std::pair as key, but std::unordered_map does not? 比较两个map :: iterators:为什么需要std :: pair的拷贝构造函数? - Comparing two map::iterators: why does it need the copy constructor of std::pair?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM