I have some code like this
#include <chrono>
using std::chrono::steady_clock;
using std::chrono::time_point;
class MyClass
{
public:
MyClass() noexcept = default;
MyClass(MyClass const&) noexcept = default;
MyClass(MyClass&&) noexcept = default;
MyClass& operator=(MyClass const&) noexcept = default;
MyClass& operator=(MyClass&&) noexcept = default;
private:
time_point<steady_clock> timestamp;
};
int main() {
MyClass myObject;
MyClass myOtherObject(myObject);
return 0;
}
Trying to compile it results in
prog.cpp: In function ‘int main()’:
prog.cpp:18:10: error: use of deleted function ‘constexpr MyClass::MyClass()’
MyClass myObject;
^~~~~~~~
prog.cpp:8:5: note: ‘constexpr MyClass::MyClass() noexcept’ is implicitly deleted because
its exception-specification does not match the implicit exception-specification ‘’
MyClass() noexcept = default;
But compiles fine if I remove the noexcept specifier for the default constructor:
MyClass() = default;
MyClass(MyClass const&) noexcept = default;
MyClass(MyClass&&) noexcept = default;
MyClass& operator=(MyClass const&) noexcept = default;
MyClass& operator=(MyClass&&) noexcept = default;
So I have 2 questions:
- How come time_point's default constructor isn't
noexcept? I mean, as far as I understand it only contains an integer of some type representing the time since epoch, which is supposed to be 0 for the default constructor if I believe cppreference - Why does removing
noexceptfrom only the default constructor work? If the compiler says that it had generatedMyClass()and notMyClass() noexceptand therefore deleted it, it should be the same forMyClass(MyClass const&), right? But the compiler lets me do the copy without complaining here...