首页 > 解决方案 > 将 nullptr 传递给 std::stoi

问题描述

对于以下参考文献中记录的 stoi 函数,

http://www.cplusplus.com/reference/string/stoi/

int stoi (const string&  str, size_t* idx = 0, int base = 10);

然后它被用作

int i_hex = std::stoi (str_hex,nullptr,16);

这里idx没有定义为常量指针(size_t * const idx =0),为什么我们可以传递nullptr作为参数呢?nullptr 不是常数吗?

标签: c++pointers

解决方案


nullptr 根本不是指针。它是类型的prvaluestd::nullptr_t。但是,它可以隐式转换为任何指针类型。指针类型是 const、volatile 等都无关紧要。所以,上面的代码没问题。


推荐阅读