Solved

Can I add a comment or line of code to a function or above so when the application is built it throws warnings if the function is used?

Posted on 2008-06-12
11
332 Views
Last Modified: 2013-12-13
I have seen something like this for Visual Studio, but have not tested it:
#pragma deprecated
http://msdn.microsoft.com/en-us/library/8wsycdzs(VS.71).aspx

Is there something similar for GCC or other compilers or do they recognize this?

It would be very useful to get warnings when we are using deprecated functions.
0
Comment
Question by:ctangent
11 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 21769931
Some versions of gcc will warn if you use deprecated C/C++ features. Unfortunately, Microsoft went a bit mad and marked functions as deprecated when they are still valid Posix/ANSI CC++ functions.

For more on gcc see this thread: { http:Q_23478482.html }
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 200 total points
ID: 21770482
I think ctangent means that he wants to do this for his own functions. I don't know of any gcc #pragma that would do that. You could do something like this though :

        void fun() {
            /* do something */
        }

        #define fun _Pragma("\"Warning: deprecated function 'fun' called\"") fun

        int main(void) {
            fun();                      /* <--- will generate the above warning */
            return 0;
        }
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21770771
>> I think ctangent means that he wants to do this for his own functions
Maybe, but I was responding to this point : "It would be very useful to get warnings when we are using deprecated functions.", which I interpreted to mean those deprecated in the standard.
0
 

Author Comment

by:ctangent
ID: 21771119
evilrix, infinity is correct. I'm interested in calling my own functions deprecated.

Infinity08, would that method work for all compilers?

So at the top of any function I could do:

#define myfunction _Pragma("WARNING HERE") myfunction
void myfunction()
{

}
for any function and it would work (even if function takes arguments etc.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 21771257
>>would that method work for all compilers?

#pragma directives are very compiler specific and not covered by any standard. GCC even discourages their usage (http://gcc.gnu.org/onlinedocs/gcc/Pragmas.html#Pragmas). If you want something portable, maybe the #error preprocessor directive would help.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 40

Expert Comment

by:evilrix
ID: 21771315
>> I'm interested in calling my own functions deprecated.
Try __attribute__ ((deprecated))
0
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 21771325
Odd... it didn't post my example! I'll try again :)

>> I'm interested in calling my own functions deprecated.
Try __attribute__ ((deprecated))

int main()

{

        float f= 1.1f;

}

rcormier@satan:~/tmp$ cat x.cpp

#include <iostream>
 

void myfunc(int i) __attribute__ ((deprecated));
 

void myfunc(int i)

{

        std::cout << "hello" << std::endl;

}
 

int main()

{

        myfunc(0);

}

Open in new window

0
 
LVL 40

Expert Comment

by:evilrix
ID: 21771332
Arrr, now I've posted too much... undo undo!

>> I'm interested in calling my own functions deprecated.
Try __attribute__ ((deprecated))

#include <iostream>
 

void myfunc(int i) __attribute__ ((deprecated));
 

void myfunc(int i)

{

        std::cout << "hello" << std::endl;

}
 

int main()

{

        myfunc(0);

}

Open in new window

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 250 total points
ID: 21771337
>> __attribute__ ((deprecated))
Documentation for all function attributes: http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 200 total points
ID: 21773954
>> Infinity08, would that method work for all compilers?

Difficult to say, since pragma's are completely implementation defined. It does for gcc - that I tested (and that was what your question was about if I'm not mistaken). Note that the _Pragma operator is part of C99.


>> So at the top of any function I could do:

No, it should not be seen by the function definition, or the function definition itself will generate the warning. Ie. you should place it in the header file AFTER the function declaration.


>> (even if function takes arguments etc.

Yes. It just uses the function name.



But, evilrix's __attribute__ ((deprecated)) solution is nicer :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21774112
>> evilrix's __attribute__ ((deprecated)) solution is nicer
Thanks :)

Yours was pretty neat too -- but I just decided to RTM ;)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now