C ++使用{}代替进行函数调用

问题描述

在C ++中,我看到了一个带有此签名功能函数

DocumentReference::DocumentReference(model::ResourcePath path,std::shared_ptr<Firestore> firestore)
    : firestore_{std::move(firestore)} {
    // code here removed for https://stackoverflow.com/
}

但是库使用{}而不是()调用函数

return DocumentReference{
    ResourcePath::FromString(document_path),shared_from_this()
};

{}而不是()调用函数有什么区别?

解决方法

这不是“调用函数”。 DocumentReference::DocumentReference是一个构造函数。构造对象的方法有很多,{}是其中一种。见

,

用{}而不是()调用函数有什么区别?

在这种情况下没有区别。两者都只是初始化DocumentReference对象。

不过最好使用{}

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es23-prefer-the--initializer-syntax