boost :: lexical_cast如何只采用一种模板类型?

0x5*_*9df 3 c++ generics templates boost template-specialization

我查看了lexical_cast.hpp的混乱,这继续逃避我.

lexical_cast,其"基本定义"同时采用模板源和目标,能够接受诸如lexical_cast<int>("7")?之类的语法?我不知道它怎么只需要一个模板化的返回类型,并且不需要你给出参数的类型而不做像部分模板特化这样的非法操作.

注意:我理解如何使用单个模板类型和不同参数的重载来完成此操作,但我无法理解lexical_cast是如何基于需要源模板类型和目标模板类型的模板函数.

GMa*_*ckG 5

模板参数可以从函数参数中推导出来:

template <typename T>
void foo(const T& x)
{
    // T is the type of X
}

foo(5); // T is be int
foo("hello"); //T is const char[6]
Run Code Online (Sandbox Code Playgroud)