我在使用时遇到问题noexcept
派生类上的说明符,更准确地说,当父类是抽象类时(具有protected
构造函数)。
以下是我声明类的方式的示例。
- With a
public
基类中的构造函数:一切正常。
- 与相同的代码
protected
并且派生类不再是“不可抛出可移动的”。
我错过了什么吗?是std::is_nothrow_move_constructible
在派生类声明中使用的正确特征还是我应该使用其他东西?
#include <cstdlib>
#include <iostream>
class BaseOk
{
public:
BaseOk ( BaseOk&& other ) noexcept {}
};
class BaseNok
{
protected:
BaseNok ( BaseNok&& other ) noexcept {}
};
class ChildOk : public BaseOk
{
public:
ChildOk ( ChildOk&& other ) noexcept ( std::is_nothrow_move_constructible < BaseOk >::value )
: BaseOk ( std::move ( other ) ) {}
};
class ChildNok : public BaseNok
{
public:
ChildNok ( ChildNok&& other ) noexcept ( std::is_nothrow_move_constructible < BaseNok >::value )
: BaseNok ( std::move ( other ) ) {}
};
int main ()
{
std::cout << std::boolalpha;
std::cout << "Is BaseOk move constructible? " << std::is_move_constructible < BaseOk >::value << '\n';
std::cout << "Is ChildOk move constructible? " << std::is_move_constructible < ChildOk >::value << '\n';
std::cout << '\n';
std::cout << "Is BaseOk nothrow move constructible? " << std::is_nothrow_move_constructible < BaseOk >::value << '\n';
std::cout << "Is ChildOk nothrow move constructible? " << std::is_nothrow_move_constructible < ChildOk >::value << '\n';
std::cout << '\n';
std::cout << "Is BaseNok move constructible? " << std::is_move_constructible < BaseNok >::value << '\n';
std::cout << "Is ChildNok move constructible? " << std::is_move_constructible < ChildNok >::value << '\n';
std::cout << '\n';
std::cout << "Is BaseNok nothrow move constructible? " << std::is_nothrow_move_constructible < BaseNok >::value << '\n';
std::cout << "Is ChildNok nothrow move constructible? " << std::is_nothrow_move_constructible < ChildNok >::value << '\n';
std::cout << std::endl;
return EXIT_SUCCESS;
}
Output:
Is BaseOk move constructible? true
Is ChildOk move constructible? true
Is BaseOk nothrow move constructible? true
Is ChildOk nothrow move constructible? true
Is BaseNok move constructible? false
Is ChildNok move constructible? true
Is BaseNok nothrow move constructible? false
Is ChildNok nothrow move constructible? false
___ 编辑 ____________________________________________________________
经过一段时间的搜索,并考虑到答案奥列格·波格丹诺夫,不幸的是似乎无法结合protected
构造函数使用noexcept ( is_nothrow_... )
.
我正在编写抽象类和声明的构造函数protected
仅用于文档目的。现在,建设者又回到了public
但我面临另一个问题:
由于抽象类无法实例化,std::is_nothrow_move_constructible<BaseClass>
回报false
并且所有派生类永远不能被标记为不抛出异常,即使它们没有抛出异常。
请参阅下面的示例:
#include <cstdlib>
#include <iostream>
class Foo
{
public:
Foo ( Foo&& other ) noexcept {}
virtual ~Foo () = 0; // Removing '= 0' makes both outputs print 'true'.
};
Foo::~Foo () {}
class Bar : public Foo
{
public:
Bar ( Bar&& other ) noexcept ( std::is_nothrow_move_constructible < Foo >::value )
: Foo ( std::move ( other ) ) {}
};
int main ()
{
std::cout << std::boolalpha;
std::cout << "Foo: " << std::is_nothrow_move_constructible < Foo >::value << '\n';
std::cout << "Bar: " << std::is_nothrow_move_constructible < Bar >::value << '\n';
return EXIT_SUCCESS;
}
Output:
Foo: false
Bar: false