相关疑难解决方法(0)

确定抽象基类的构造函数是否为noexcept?

在C++ 11及更高版本中,如何确定抽象基类的构造函数是noexcept?以下方法不起作用:

#include <new>
#include <type_traits>
#include <utility>

struct Base { Base() noexcept; virtual int f() = 0; };

// static assertion fails, because !std::is_constructible<Base>::value:
static_assert(std::is_nothrow_constructible<Base>::value, "");

// static assertion fails, because !std::is_constructible<Base>::value:
static_assert(std::is_nothrow_default_constructible<Base>::value, "");

// invalid cast to abstract class type 'Base':
static_assert(noexcept(Base()), "");

// invalid new-expression of abstract class type 'Base'
static_assert(noexcept(new (std::declval<void *>()) Base()), "");

// cannot call constructor 'Base::Base' directly:
static_assert(noexcept(Base::Base()), "");

// invalid use of 'Base::Base':
static_assert(noexcept(std::declval<Base &>().Base()), "");
Run Code Online (Sandbox Code Playgroud)

一个简单的用途是:

int g() noexcept; …
Run Code Online (Sandbox Code Playgroud)

c++ noexcept c++11 c++14 c++17

19
推荐指数
2
解决办法
896
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

c++17 ×1

noexcept ×1