1

I know about similar questions, but still would like to understand if described warning is pointless or not.

Potential duplicates could be:

but I still have a feeling, this question is different.

Let's imagine that I have a base class implemented as NVI.

  struct server_strategy
  {
    explicit server_strategy(std::string name) : name_{std::move(name)} {}
    virtual ~server_strategy()=default;

    zmq::message_t handle_message(zmq::message_t request)
    {
      // ...
    }

    std::string const& log_channel()const {return name_;}

  private:
    virtual zmq::message_t do_handle_message(zmq::message_t request) =0;


  private:
    const std::string name_;
  };


Compiling this class makes Clang issuing the warning:

error: 'server_strategy' has no out-of-line virtual method definitions;
its vtable will be emitted in every translation unit [-Werror,-Wweak-vtables]

Question is about =default keyword and not about where virtual destructor's impl will finally land!

  • Why does =default makes sense at all in context of virtual destructors?
  • Don't you think that this warning is overkill in that context?

I mean if I explicitly declare virtual destructor as defaulted than I probably don't want to introduce an additional .cpp file and I know that compiler is going to pick any translation unit for its implementation. It'd be handy to let library authors avoid these warnings, but switching to defaulted virtual destructors instead of inline implementation, but it turns out that library authors have no other choice than deal with pragmas and conditional define macros which compiler is being used to disable these warnings.

ovanes
  • 5,483
  • 2
  • 34
  • 60
  • Why both c++11 and c++14? – klutt May 26 '17 at 20:07
  • @klutt `=default` was introduced in C++11 and applies for C++14 as well. This is my understanding of tagging, that some information is associated with particular context. This warning will be issued in C++11 and 14. – ovanes May 26 '17 at 20:08
  • 2
    This question is no different from [What is the meaning of clang's -Wweak-vtables?](https://stackoverflow.com/questions/23746941/what-is-the-meaning-of-clangs-wweak-vtables). You have a class for which a vtable needs to be generated, but no corresponding .cpp file. A vtable is a physical piece of data, it needs to be generated *somewhere*. If the compiler can't find that place, as a workaround it generates it *everywhere*, which obviously isn't nice, hence the warning. – rustyx May 26 '17 at 20:24
  • In this question is not a single word about `=default`. I feel like explicitly defaulted virtual dtor should no issue this warning. – ovanes May 26 '17 at 20:27
  • From the compiler's standpoint it has the same code generation problem whether the implementation is inline in the header or defaulted. You can just pretend that one or more of the `{}` implementations in the duplicate are `=default` instead. – Mark B May 26 '17 at 20:28
  • @MarkB: Why can't compiler save additional meta-info for later phases and decide whether to show this warning or not? – ovanes May 26 '17 at 20:35
  • I still don't see the question as duplicate. – ovanes May 26 '17 at 20:36
  • 1
    @ovanes Is there a reason you don't want to use `-Wno-weak-vtables` to suppress the warning? – Mark B May 26 '17 at 20:37
  • 1
    @MarkB Why should I? Warnings are there to warn me about smth. important. I can disable any warning and think my code is great and clean. I can even ask compiler to output the result despite errors. But I really want to see what's potentially wrong (what I might missed) with my code and these kinds of warnings really make just noise :(, because I think I am explicit here, that I know what I do. I remember being at BoostCon 2010, when Doug Gregor stated that Clang shouldn't require any preprocessor hacks and cleanly support all language features, but now it turns out that it does. – ovanes May 26 '17 at 20:40

0 Answers0