相关疑难解决方法(0)

为什么`std :: sto` ...系列不是模板?

我不知道是否有一个原因的std::sto系列(例如std::stoi,std::stol)不是一个函数模板,这样的:

template<typename T>
T sto(std::string const & str, std::size_t *pos = 0, int base = 10);
Run Code Online (Sandbox Code Playgroud)

然后:

template<>
int sto<int>(std::string const & str, std::size_t *pos, int base)
{
    // do the stuff.
}

template<>
long sto<long>(std::string const & str, std::size_t *pos, int base)
{
    // do the stuff.
}

/* etc. */
Run Code Online (Sandbox Code Playgroud)

在我看来,这将是一个更好的设计,因为目前,当我必须转换用户想要的任何数值的字符串时,我必须手动管理每个案例.

有没有理由没有这样的模板功能?是否有一个假定的选择,或者这样做是否就是这样?

c++ c++11

19
推荐指数
3
解决办法
2072
查看次数

标签 统计

c++ ×1

c++11 ×1