代码之家  ›  专栏  ›  技术社区  ›  Nordlöw

C++类构造函数,其为:γ-AtIdTyth*((纯))或γ-AbjiTyx((const))

  •  2
  • Nordlöw  · 技术社区  · 7 年前

    __attribute__((pure)) 如果他们只能通过其参数访问数据?在哪些情况下,他们应被认定为 __attribute__((const)) ?

    1 回复  |  直到 7 年前
        1
  •  2
  •   P.W    7 年前

    当您将构造函数限定为 pure const . 这是因为构造函数不返回任何内容(返回 void 纯净的 或 常数 这些函数的属性。

    请参阅godbolt演示 here .

    <source>:3:30: warning: 'pure' attribute on function returning 'void' [-Wattributes]
         A()  __attribute__((pure));
    
                                  ^
    <source>:8:31: warning: 'const' attribute on function returning 'void' [-Wattributes]
         B()  __attribute__((const));                               ^
    

    documentation :


    ...
    因为const函数不能有任何副作用,所以这种函数返回void是没有意义的。声明这样一个函数就是诊断。

    纯净的

    因为纯函数不会有任何副作用,所以这种函数返回void是没有意义的。声明这样一个函数就是诊断。