Why does C++ code missing a formal argument name in a function definition compile without warnings?

前端 未结 4 750
闹比i
闹比i 2020-11-29 01:44

While getting started with some VS2005-generated MFC code, I noticed it overrode a method with something like this:

void OnDraw(CDC* /*pDC*/)
{
    ...
    /         


        
4条回答
  •  清歌不尽
    2020-11-29 02:27

    C++11 N3337 standard draft

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf says it is legal at 8.4.1/6 "Function definitions > In general":

    Note: Unused parameters need not be named. For example,

    void print(int a, int) {
        std::printf("a = %d\n",a);
    }
    

    More precisely, 8.4.1/1 says that the grammar for function definitions is

    function-definition:
        attribute-specifier-seqopt decl-specifier-seqopt
        declarator virt-specifier-seqopt function-body
    

    Then if you follow the grammar definitions, e.g. under "Annex A Grammar summary", you will see that the names are optional.

提交回复
热议问题