0

when checking a functor which is derived from std::unary_function as follows

struct IsInterestingMsg : public std::unary_function<string,bool>

Lint ejects the following info/warnings:

1790: Base class 'std::unary_function<std::basic_string<char>,bool>' has no non-destructor virtual functions

and

Warning 1509: base class destructor for class 'unary_function' is not virtual

According to Scott Meyers, functor classes should be made adaptable by deriving from unary/binary_function which are basically only a collection of typedefs, thus, they are no classes that need any constructor / destructor. Therefore, the lint warnings are per se correct.

Does anybody know how to suppress these warnings globally and only for all usage of unary_function etc.? I want to avoid writing an -e1509 every time it's used.

Info #1790 can be suppressed by using private inheritance instead of public, but warning #1509 remains.

4

2 回答 2

1

Sigh, lint should have learned that... Anyhow, what you gain by deriving from unary_function is a few nested typedefs, see http://www.cplusplus.com/reference/functional/unary_function/. You can create these yourself instead of inheriting them.

Another way would be to conditionally declare a virtual destructor in unary_function when lint is running. I personally wouldn't compromise perfectly valid code for the sake of lint though, neither in one way nor the other.

于 2013-04-12T17:51:09.157 回答
1

The following suppression works for me:

/*lint -esym(1790, "std::unary_function<*,*>") -esym(1509, unary_function) */

There are a few pitfalls here:

  • Note the quotes for the argument to esym, so Lint doesn't interpret the comma in the template parameter list
  • Message 1790 is parameterized on a "Symbol" - thus you have to use the full namespace when refering to it
  • Message 1509 on the other hand is parameterized on a "Name" so you're not supposed to give the complete namespace with it

As for when to use the namespace and when to omit it, just use the string that is quoted in Lint's error message, and it should be fine. The downside is that there doesn't seem to be a way to disable 1509 for std::unary_function, but not for ns::unary_function.

于 2013-04-15T12:40:29.960 回答