我试图更好地理解构造函数背后in_place_t
的动机。std::optional<T>
我得到用例来消除使用默认构造函数构建null
可选与类型的歧义。T
但是,如果为std::optional
构造函数指定了一个或多个参数,那么不能假设它们总是可以转发给构造函数T
吗?
#include <optional>
struct X
{
X(int, int) {}
};
int main()
{
std::optional<X> op{std::in_place, 1,2};
//std::optional<X> op{1,2}; Does not compile, but why can't it be assumed that 1,2 always go to the constructor of X
}