mercredi 11 octobre 2017

Handling "Thrown exception type is not nothrow copy constructible" Warning

Going back to C++ development after a 12 years hiatus. I'm using JetBrains's CLion software which is great since it provides a lot of input on possible issues on my class design. One of the warning I get in my class' constructor throw statement is: Thrown exception type is not nothrow copy constructible. Here is a code sample that generates this warning:

#include <exception>
#include <iostream>

using std::invalid_argument;
using std::string;

class MyClass {
    public:
        explicit MyClass(string value) throw (invalid_argument);
    private:
        string value;
};

MyClass::MyClass(string value) throw (invalid_argument) {
    if (value.length() == 0) {
        throw invalid_argument("YOLO!"); // Warning is here.
    }

    this->value = value;
} 

This piece of code compiles and I am able to unit test it. But I would like very much to get rid of this warning (in order to understand what I am doing wrong, even though it compiles).

Thanks

Aucun commentaire:

Enregistrer un commentaire