Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::pair not nothrow constructible?

The following code:

#include <iostream>
#include <iomanip>
#include <string>
#include <utility>
using namespace std;

struct Foo
{
    std::string s;
    int i;
};

int main()
{
    cout << boolalpha << is_nothrow_constructible<Foo>::value << endl;
    cout << is_nothrow_constructible<pair<string, int>>::value << endl;

    cout << is_nothrow_move_constructible<Foo>::value << endl;
    cout << is_nothrow_move_constructible<pair<string, int>>::value << endl;

    return 0;
}

produces the following output when compiled with g++ -std=c++11:

true
false
true
true

Why is std::pair<string, int> not nothrow constructible, while Foo is, and why it is nothrow move constructible?

like image 320
Nick Avatar asked Oct 17 '22 17:10

Nick


1 Answers

Interestingly enough, none of the constructors is declared noexcept under any conditions. Likely a typical Standard defect (only the textual description below promises to not throw any exceptions if none of the elements does.)

like image 127
bipll Avatar answered Nov 15 '22 05:11

bipll