Is it legal to pass a newly constructed object by reference to a function?

后端 未结 9 2162
刺人心
刺人心 2020-12-31 04:44

Specifically, is the following legal C++?

class A{};

void foo(A*);
void bar(const A&);

int main(void)
{
    foo(&A());  // 1
    bar(A());  // 2
}

It a

9条回答
  •  没有蜡笔的小新
    2020-12-31 05:35

    It is legal. We use it sometime to provide a default value which we might want to ignore.

    int dosomething(error_code& _e = ignore_errorcode()) {
        //do something
    }
    

    In the above case it will construct an empty error code object if no error_code is passed to the function.

提交回复
热议问题