【问题标题】:pragma GCC poison syntax for member function成员函数的 pragma GCC 毒药语法
【发布时间】:2019-08-25 03:54:56
【问题描述】:

在 GCC(和 clang)中有这个选项可以毒化函数:

#include <stdio.h>
#pragma GCC poison puts

int main() {
    puts("a");
}

毒化模板类的成员函数的语法是什么?

我尝试了以下选项,我什至没有设法使其适用于非模板类成员。

#include <stdio.h>
#pragma GCC poison puts

struct A{
  bool operator==(A const& o){return true;}
};

#pragma GCC poison A::operator== //not working

template<class T>
struct B{
  bool operator==(B const& o){return true;}
};

#pragma GCC poison template<class T> B<T>::operator== //not working either


int main() {
    puts("a");
}

https://godbolt.org/z/rBEgjZ

【问题讨论】:

    标签: c++ gcc compiler-errors pragma


    【解决方案1】:

    我不认为有这样的语法。 documentation 表示 #pragma GCC poison 是预处理器本身的一部分;事实上,GCC 文档甚至都没有提到它。

    这意味着它只适用于预处理器理解的东西,即标识符标记。像A::operator== 这样的东西是四个独立的标记:A::operator==。其中你只能毒死Aoperator;预处理器不理解范围或类,更不用说模板了。

    【讨论】:

    • 是的,在示例中它们总是毒化非常低级的函数。也许必须为预处理器输入解构函数的名称才能识别该函数。我会继续使用[[deprecated("msg")]],直到找到更好的选择。
    • @alfC 预处理器不知道函数,只知道标识符。你甚至不能说{ int puts; }
    • 啊,好吧,所以原则上我也可以毒化任何有效的标识符和类型?
    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 2016-12-27
    • 2023-03-23
    • 2017-09-19
    • 2011-06-17
    相关资源
    最近更新 更多