“处理”;抛出的异常类型不是nothrow copy constructible“;警告 >经过12年的中断后回到C++开发。我正在使用JetBrains的CLion软件,它非常棒,因为它为我的课堂设计中可能出现的问题提供了大量的输入。我在类的构造函数throw语句中得到的一个警告是:抛出的异常类型不是nothrow copy constructible。以下是生成此警告的代码示例: #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; } #包括 #包括 使用std::无效的_参数; 使用std::string; 类MyClass{ 公众: 显式MyClass(字符串值)抛出(无效的_参数); 私人: 字符串值; }; MyClass::MyClass(字符串值)抛出(无效的\u参数){ 如果(value.length()==0){ 抛出无效的_参数(“YOLO!”);//警告在这里。 } 这个->值=值; }

“处理”;抛出的异常类型不是nothrow copy constructible“;警告 >经过12年的中断后回到C++开发。我正在使用JetBrains的CLion软件,它非常棒,因为它为我的课堂设计中可能出现的问题提供了大量的输入。我在类的构造函数throw语句中得到的一个警告是:抛出的异常类型不是nothrow copy constructible。以下是生成此警告的代码示例: #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; } #包括 #包括 使用std::无效的_参数; 使用std::string; 类MyClass{ 公众: 显式MyClass(字符串值)抛出(无效的_参数); 私人: 字符串值; }; MyClass::MyClass(字符串值)抛出(无效的\u参数){ 如果(value.length()==0){ 抛出无效的_参数(“YOLO!”);//警告在这里。 } 这个->值=值; },c++,c++11,exception,std,clang-tidy,C++,C++11,Exception,Std,Clang Tidy,这段代码经过编译,我能够对其进行单元测试。但是我非常想摆脱这个警告(为了理解我做错了什么,即使它是编译的)。尼尔提供的评论是有效的。在C++ 11中,函数签名中使用投掷< /代码>已被弃用,而不是代码>。在这种情况下,我的构造函数的签名应该是: explicit MyClass(string value) noexcept(false); 但是,由于默认情况下,noexcept(false)应用于所有函数,除非指定了noexcept或noexcept(true),否则我可以简单地使用: exp

这段代码经过编译,我能够对其进行单元测试。但是我非常想摆脱这个警告(为了理解我做错了什么,即使它是编译的)。

尼尔提供的评论是有效的。在C++ 11中,函数签名中使用<代码>投掷< /代码>已被弃用,而不是<>代码>。在这种情况下,我的构造函数的签名应该是:

explicit MyClass(string value) noexcept(false);
但是,由于默认情况下,
noexcept(false)
应用于所有函数,除非指定了
noexcept
noexcept(true)
,否则我可以简单地使用:

explicit MyClass(string value);

回到如何修复“抛出的异常类型不是NOTHOW copy constructible”警告,我发现这很好地解释了问题所在以及如何修复它。

为什么要使用抛出规范?它们已被弃用。痛苦
throw
说明符带来了很多痛苦。如果没有,你会更好。