Why is it impossible to have a reference-to-void?

后端 未结 10 947
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 19:32

Why is it impossible to have a reference to void? The only thing I found in the C++ Standard is this line, at 8.3.2.1

A declarator th

10条回答
  •  礼貌的吻别
    2020-12-23 20:13

    The following is not a defense of the notion of void references. I offer it as an anecdote from the wild. Ask yourself if it doesn't smell funny.

    My company was one of the first using C++ commercially, and initially compiled using Cfront. Early developers were still learning the language, and generally using every trick in the book (operators everywhere!). Here is a trick they thought was cool:

    void Foo::something(int action, ostream &os = *(ostream *)0)
    {
       ostream *os_p = &os;
       if (&os == (ostream *)0) {
          os_p = &cerr;
       }
       // continue with method
    }
    

    So here you have, not a void reference, but rather a typed reference with a potentially void binding! A moment's thought should probably suggest better alternatives to this particular idiom...

提交回复
热议问题