构造函数是否具有“类型”既然它是一个特殊的成员函数?

发布于 2025-01-18 23:33:47 字数 463 浏览 2 评论 0 原文

我最近了解到构造函数没有名称。我还知道函数有一个称为函数类型的类型。例如,

void func(int)
{
}

在上面的代码片段中,func 具有函数类型 void (int)

现在,既然构造函数是特殊的成员函数,那么它们是否也有像上面所示的类型呢?例如,假设我们有:

struct Name
{ 
    Name(int)
    {
    }
};

上面显示的构造函数是否也像普通函数或普通成员函数一样具有函数类型?如果是,那么我们如何找到该类型呢?是否允许在构造函数上使用 decltype 来查找其类型,就像对普通函数所做的那样?

I recently learnt that constructors do not have names. I am also aware that a function has a type called a function type. For example,

void func(int)
{
}

In the above snippet, func has the function type void (int).

Now, since constructors are special member functions, do they also have a type like the one shown above? For example say we have:

struct Name
{ 
    Name(int)
    {
    }
};

Does the constructor shown above also have a function type just like ordinary functions or ordinary member functions? If yes, then how can we find that type? Is it permitted to use decltype on constructors to find their type, like one would do for ordinary functions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2025-01-25 23:33:47

是否允许在构造函数上使用 secttype 查找其类型

它是否允许 。主要是因为没有办法命名构造函数。一个常见的错误表明是 name(0) new Name(0),调用构造函数的表达式。但事实并非如此,例如 func(0)中的情况。我们从来没有直接调用构造函数,而是始终是由需要新对象出现的语言构造而间接的。

[class.ctor.general]

1 ...构造函数没有构造函数名称。

2 构造函数用于初始化对象其班级类型。因为构造函数没有名称,所以在名称查找中从未找到它们。但是,使用功能符号([expr.type.conv])的显式类型转换将导致调用构造函数以初始化对象。
[注1:语法看起来像是构造函数的明确调用。 - 终点注]

因为我们无法命名它们,所以我们无法使用 exltype 之类的内省机制来检查它们。因此,该标准没有为构造函数指定“类型”,因为严格符合标准的程序无法检查该类型。

构造函数也不能具有签名(标准定义),因为根据定义,构造函数包括函数名称(如提到的构造函数是无名的)。

[defns.signature.member] 签名 P>

class成员函数⟩名称,参数型列表,该类是函数的类,cv-qualifier(如果有),ref-qualifier(如果有)和尾随需要clause(如果有)

is it permitted to use decltype on constructors to find their type

It's not permitted. Primarily because there is no way to name a constructor. A common misnomer is that an expression like Name(0) or new Name(0), calls the constructor. But that isn't the case like in func(0). A constructor is never called by us directly, but rather always indirectly by the language construct that requires a new object to come into being.

[class.ctor.general]

1 ... Constructors do not have names.

2 A constructor is used to initialize objects of its class type. Because constructors do not have names, they are never found during name lookup; however an explicit type conversion using the functional notation ([expr.type.conv]) will cause a constructor to be called to initialize an object.
[Note 1: The syntax looks like an explicit call of the constructor. — end note]

Because we cannot name them, we cannot use introspection mechanisms like decltype to examine them. Therefore the standard doesn't specify a "type" for constructors, since there is no way for a strictly standard compliant program to examine said type.

A constructor also cannot possess a signature (as defined by the standard), since that by definition includes the function name (and constructors are, as mentioned, nameless).

[defns.signature.member] signature

⟨class member function⟩ name, parameter-type-list, class of which the function is a member, cv-qualifiers (if any), ref-qualifier (if any), and trailing requires-clause (if any)

一百个冬季 2025-01-25 23:33:47

构造函数是否有“类型”

这是 CWG2476,其状态为 DRWP(在 C++26 工作草案中接受的意思)这也使得构造函数具有类型。注意已发布 c++23不允许构造函数具有类型:

C++23

这里 T D 中的 T 不允许为空,这意味着构造函数在 C++ 中没有类型23 已发布。

来自 dcl.fct

  1. 在声明 T D 中,其中 D 具有以下形式
 D1 (参数声明子句) cv-qualifier-seqopt
   ref-qualifieropt noexcept-specifieropt 属性说明符-seqopt
  • 在声明 TD 中,其中 D 具有以下形式
  •  D1 (参数声明子句) cv-qualifier-seqopt
       ref-qualifieropt noexcept-specifieropt 属性说明符-seqopt 尾随返回类型  
    

    可选的 attribute-specifier-seq 属于函数类型。

  • 任何一种形式的类型都是函数类型。
  • 这里注意TCWG2476 表示构造函数在 C++23 中没有类型。


    CWG2476

    在 C++26 的工作草案中,T 可以为空,这意味着现在构造函数确实有一个类型。

    [在 2024 年 3 月的会议上被接受为 DR。]

  • 对 9.3.4.6 [dcl.fct] 第 1 段的更改如下:
  • 在声明 TD 中其中 T 可以为空并且 D 具有以下形式:

    D1(参数声明子句)cv-qualifier-seqopt
     ref-qualifieropt noexcept-specifieropt 属性说明符-seqopt Trailing-return-typeopt
    

    任一形式的类型都是函数类型

    请注意,在上面的 CWG 中添加了对“T 可能为空”的强调,这意味着构造函数 Name::Name(int) 确实有一个类型。

    Does a constructor have a "type"

    This is CWG2476 whose status is DRWP(meaning accepted in C++26 working draft) which also makes constructors have a type. Note as published c++23 did not allow constructors to have a type:

    C++23

    Here T in T D is not allowed to be empty meaning a constructor doesn't have a type in C++23 as published.

    From dcl.fct:

    1. In a declaration T D where D has the form
     D1 ( parameter-declaration-clause ) cv-qualifier-seqopt
       ref-qualifieropt noexcept-specifieropt attribute-specifier-seqopt
    
    1. In a declaration T D where D has the form
     D1 ( parameter-declaration-clause ) cv-qualifier-seqopt
       ref-qualifieropt noexcept-specifieropt attribute-specifier-seqopt trailing-return-type  
    

    The optional attribute-specifier-seq appertains to the function type.

    1. A type of either form is a function type.

    Note here T was not allowed to be empty before CWG2476 meaning a constructor function doesn't have a type in C++23.


    CWG2476

    In the working draft of C++26, T can be empty, meaning now constructors do have a type.

    [Accepted as a DR at the March, 2024 meeting.]

    1. Change in 9.3.4.6 [dcl.fct] paragraph 1 as follows:

    In a declaration T D where T may be empty and D has the form :

    D1 ( parameter-declaration-clause ) cv-qualifier-seqopt
     ref-qualifieropt noexcept-specifieropt attribute-specifier-seqopt trailing-return-typeopt
    

    A type of either form is a function type.

    Note the emphasis on T may be empty added in the above CWG which means the constructor Name::Name(int) does have a type.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文