1

Is there any reasonable and portable way to generate a compiler warning in C++ when a client of the library I am providing uses a specific specialization of a template class that we want to discourage?

In contrast to the questions that this one has been marked as a duplicate of, I only need a warning, not a compile time error.

languitar
  • 6,554
  • 2
  • 37
  • 62
  • Take a look at [the proposal for the C++1y deprecated attribute](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3760.html). It's obviously too soon for C++1y features to be useful, but it does detail the current compiler-specific stuff nicely. – Casey Feb 18 '14 at 18:20
  • Unfortunately, there is no such thing as a portable compiler warning, since the Standard doesn't require any such thing except via `#warning`, which is preprocessor-time. – aschepler Feb 18 '14 at 18:44
  • 1
    Oops, `#warning` isn't standard at all. – aschepler Feb 18 '14 at 18:53
  • There are reasonable and portable ways to generate an error, but not a warning. – Alan Stokes Feb 18 '14 at 20:02
  • Look here for some funky ideas: http://stackoverflow.com/questions/8936063/does-there-exist-a-static-warning – Christopher Oicles Feb 18 '14 at 20:14
  • @ArthurChamz, your questions deals with generating an error, which seems to be much easier – languitar Feb 19 '14 at 13:56
  • @languitar My bad, I thought it would serve your purposes. – ArthurChamz Feb 19 '14 at 15:16

1 Answers1

0

I could reuse the STATIC_WARNING_TEMPLATE from Does there exist a static_warning? proposed by in combination with boost::is_same to generate a warning when using a specific template parameter:

template<class T, class DisableWarning = void>
class Foo {
public:
    STATIC_WARNING_TEMPLATE(FOO_WARNING,
            (boost::is_same<DisableWarning, DislikedType>::value || !boost::is_same<T, DislikedType>::value),
            "A warning")

With this solutions user can disable the warning when defining DisableWarning as the DislikedType.

languitar
  • 6,554
  • 2
  • 37
  • 62