C/CXX attribute

nodiscard:若返回值被舍弃,则鼓励编译器发布警告。

  1. 语法
// C++17
[[nodiscard]]
[[__nodiscard__]]

// C++20
[[nodiscard(字符串字面值)]]
[[__nodiscard__(字符串字面值)]]
字符串字面量	-	能用于解释结果不应被舍弃的理由的文本
  1. 解释
  2. 出现位置是函数声明,枚举/结构体/联合声明中
  3. 若需要忽略该返回值,需要类型转换
// nodiscard限制函数返回值
[[nodiscard]] int type_get() {
    return 1;
}

type_get();  // warning

( void )type_get();  // nowarning
int t = type_get();  // nowarning

// nodiscard显示结构体
Tmp tmp_get() {
    return Tmp{};
}

tmp_get();                     // warning
    
auto t2 = tmp_get();           // nowarning
static_cast<void>(tmp_get());  // nowarning
  1. 使用场景

    1. 关键函数的返回值,比如判断空
    2. 关键资源的使用,比如内存分配
    // Macro to warn about unused results.
    #if __cplusplus >= 201703L
    # define _GLIBCXX_NODISCARD [[__nodiscard__]]
    #else
    # define _GLIBCXX_NODISCARD
    #endif
    
    _GLIBCXX_NODISCARD void* operator new(std::size_t) _GLIBCXX_THROW (std::bad_alloc)
      __attribute__((__externally_visible__));
    _GLIBCXX_NODISCARD void* operator new[](std::size_t) _GLIBCXX_THROW (std::bad_alloc)
      __attribute__((__externally_visible__));
    
    _GLIBCXX_NODISCARD bool empty() const _GLIBCXX_NOEXCEPT { return _M_t.empty(); }
    

deprecated:指示声明有此属性的名字或实体被弃用

  1. 语法

    // C++14
    [[deprecated]]	(1)	
    [[deprecated( 字符串字面量 )]]	(2)	
    字符串字面量	-	能用于解释弃用的理由并/或提议代替用实体的文本
    
  2. 解释

    1. 指示允许使用声明有此属性的名称或实体,但因故不鼓励使用。编译器通常会对其使用情况发出警告。若指定了 字符串字面量,则它通常被包含于警告中。

    2. 下列名字或实体的声明中允许使用这个属性:

      类型 用法
      class/struct/union/enum struct [[deprecated]] S;
      typedef 名 using PS [[deprecated]] = S*;
      变量 [[deprecated]] int a;
      成员变量 unicon U {[[deprecated]] int n;};
      函数 [[deprecated]] void f();
      命名空间 namespace [[deprecated]] NS{int a;};
      模板特化 template<> struct [[deprecated]] X {};
      [[deprecated]] int data_get() {
          return 1;
      }
      
      data_get();         // warning
      
  3. 使用场景

    1. 指示声明有此属性的名字或实体被弃用,即允许但因故不鼓励使用。
    2. 版本迭代过程中难免接口进行更新(不应该忽略编译警告信息???)
    #if defined(__DEPRECATED) && (__cplusplus >= 201103L)
    # define _GLIBCXX_DEPRECATED __attribute__ ((__deprecated__))
    #else
    # define _GLIBCXX_DEPRECATED
    #endif
    
      template<typename _Operation>
        class binder1st
        : public unary_function<typename _Operation::second_argument_type,
    			    typename _Operation::result_type>
        {
        protected:
          _Operation op;
          typename _Operation::first_argument_type value;
    
        public:
          binder1st(const _Operation& __x,
    		const typename _Operation::first_argument_type& __y)
          : op(__x), value(__y) { }
    
          typename _Operation::result_type
          operator()(const typename _Operation::second_argument_type& __x) const
          { return op(value, __x); }
    
          // _GLIBCXX_RESOLVE_LIB_DEFECTS
          // 109.  Missing binders for non-const sequence elements
          typename _Operation::result_type
          operator()(typename _Operation::second_argument_type& __x) const
          { return op(value, __x); }
        } _GLIBCXX_DEPRECATED;
    

本文来自博客园,作者:flxx,转载请注明原文链接:https://www.cnblogs.com/faithlocus/p/15550344.html

原文地址:https://www.cnblogs.com/faithlocus/p/15550344.html