using关键字在结构C++中的用法[duplicate]

klr1opcd  于 2022-11-27  发布在  其他
关注(0)|答案(1)|浏览(114)

此问题在此处已有答案

Constructor nominated by using declaration(1个答案)
Using-declaration for base class constructors(1个答案)
3天前关闭。
我在我的header_file run.h中创建了一个如下所示的异常

struct invalid_assignment : std::runtime_error {
   using std::runtime_error::runtime_error; 
};

我不明白using std::runtime_error::runtime_error;的部分。这看起来没有必要。

ecr0jaav

ecr0jaav1#

来自C++17标准(10.3.3 using声明)
3在用作成员声明的using声明中,每个using声明符的嵌套名称说明符应命名所定义类的基类。如果using声明符命名构造函数,则其嵌套名称说明符应命名所定义类的直接基类。

16 For the purpose of overload resolution, the functions that are introduced by a using-declaration into a derived class are treated as though they were members of the derived class. In particular, the implicit this parameter shall be treated as if it were a pointer to the derived class rather than to the base class. This has no effect on the type of the function, and in all other respects the function remains a member of the base class.Likewise, constructors that are introduced by a using-declaration are treated as though they were constructors of the derived class when looking up the constructors of the derived class (6.4.3.1) or forming a set of overload candidates(16.3.1.3, 16.3.1.4, 16.3.1.7). If such a constructor is selected to perform the initialization of an object of class type, all subobjects other than the base class from which the constructor originated are implicitly initialized (15.6.3).
因此这个using声明

using std::runtime_error::runtime_error;

会在invalid_assignment类别中引入std::runtime_error类别的建构函式,就像它们是invalid_assignment类别的建构函式一样。

相关问题