为什么C ++不推荐使用警告打印两次?

问题描述 投票:3回答:1

如果我有

namespace foo {
    inline int bar() {
        return 1119;
    }
}

__attribute__((deprecated)) inline int bar() {
    return 138;
}

header.h

#include "header.h"
#include <iostream>

int main() {
    int x = bar();
    int y = foo::bar();
    std::cout << x << std::endl;
    std::cout << y << std::endl;
}

然后在source.cpp

g++ source.cpp -o deprecated-test

结果是

source.cpp: In function ‘int main()’:
source.cpp:5:17: warning: ‘int bar()’ is deprecated [-Wdeprecated-declarations]
     int x = bar();
                 ^
In file included from source.cpp:1:
header.h:7:40: note: declared here
 __attribute__((deprecated)) int bar() {
                                 ^~~
source.cpp:5:17: warning: ‘int bar()’ is deprecated [-Wdeprecated-declarations]
     int x = bar();
                 ^
In file included from source.cpp:1:
header.h:7:40: note: declared here
 __attribute__((deprecated)) int bar() {

(在Ubuntu 18.10上使用g ++ 8.2.0)。

为什么弃用的警告打印两次?

提出一些无益的建议:

  • [[deprecated]]:我知道在C ++ 14上你可以使用[[deprecated]]属性,但我需要使用C ++ 11。
  • 声明与定义:The docs似乎暗示它应该与函数声明而不是定义一起使用,但是 我需要在头文件中定义函数inline而不是在头文件中声明并在源文件中定义;和 尝试这种方法并没有阻止打印两次警告。
c++ gcc g++
1个回答
1
投票

根据GCC 8.2.0的文档:

如果在源文件中的任何位置使用该函数,则deprecated属性会生成警告。在识别预期在程序的未来版本中删除的函数时,这非常有用。警告还包括已弃用函数声明的位置,以使用户能够轻松找到有关函数被弃用的原因的更多信息,或者他们应该做什么。请注意,警告仅适用于使用...

应该只有一个警告,而不是两个。所以这是GCC中的一个错误。

Type属性(而不是Function属性)的相关错误标题为:C/C++ __attribute__((deprecated)) does not appear to wrap declarations as implied from the doc.

它已被确认为一个错误。

© www.soinside.com 2019 - 2024. All rights reserved.