Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do C++ deprecated warnings print twice?

Tags:

c++

gcc

g++

If I have

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

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

in header.h and

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

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

in source.cpp, then

g++ source.cpp -o deprecated-test

results in

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() {

(on Ubuntu 18.10 with g++ 8.2.0).

Why does the deprecated warning print twice?

Heading off some suggestions that would be unhelpful:

  • [[deprecated]]: I know with C++14 on you can use the [[deprecated]] attribute, but I need to work with C++11.

  • Declaration vs. definition: The docs seem to imply it should be used with function declaration rather than definition, but

    1. I need to define the functions inline in a header rather than declare in the header and define in source files; and
    2. Trying this approach didn't stop the warning from printing twice anyway.
like image 625
duckmayr Avatar asked Nov 06 '22 18:11

duckmayr


1 Answers

As per the documentation of GCC 8.2.0:

The deprecated attribute results in a warning if the function is used anywhere in the source file. This is useful when identifying functions that are expected to be removed in a future version of a program. The warning also includes the location of the declaration of the deprecated function, to enable users to easily find further information about why the function is deprecated, or what they should do instead. Note that the warnings only occurs for uses...

There should be only one warning and not two. So this is a bug in GCC.

There is a related bug for Type attributes (rather than Function attributes) titled: C/C++ __attribute__((deprecated)) does not appear to wrap declarations as implied from the doc.

It has been confirmed as a bug.

like image 56
P.W Avatar answered Nov 09 '22 23:11

P.W