简体   繁体   English

关于C++中std::string类型到模板T类型的转换

[英]On the conversion from std::string type to template T type in C++

I've found on this forum the following snippet witten by user Ben Voigt:我在这个论坛上发现了以下由用户 Ben Voigt 编写的片段:

//forward declaration
template<typename T>
T getline_as(std::istream& s);

template<>
std::string getline_as<std::string>(std::istream& s)
{
    std::string str;
    std::getline(s,str);
    return str;
} 

template<typename T>
T getline_as(std::istream& s)
{
    std::stringstream convert(getline_as<std::string>(s));

    T value;
    convert >> value;
    return value;
}

I am looking for a way to handle possible failures of convert >> value;我正在寻找一种方法来处理convert >> value; . . My goal would be to iterate the request to the user to make it insert a given input correctly:我的目标是迭代对用户的请求,使其正确插入给定的输入:

int main()
{
    std::cout << "Please enter a number: ";
    double number{getline_as<double>(std::cin)};
}

My idea was to create a do-while loop inside getline_as but I can't make it work.我的想法是在getline_as中创建一个do-while循环,但我无法让它工作。

template<typename T>
T getline_as(std::istream& s)
{
    std::stringstream convert;
    T value;
    do
    {
        convert(getline_as<std::string>(s));
    }
    while(!(convert >> value));
    return value;
}

std::stringstream convert(...); is a constructor call, but trying to do convert(...);是一个构造函数调用,但试图做convert(...); after the stream is created is illegal (it would require the stream to overload operator() , which it doesn't do).创建 stream 之后是非法的(它需要 stream 重载operator() ,但它不会这样做)。 convert = std::stringstream(...) would work, but I'd just completely recreate the stream. convert = std::stringstream(...)会起作用,但我只是完全重新创建 stream。

You also should use a read-only std::istringstream , since you never write anything to it.您还应该使用只读的std::istringstream ,因为您永远不会向它写入任何内容。

I also made some cosmetic changes and ended up with following:我还进行了一些外观上的更改,最终得到以下结果:

template <typename T>
[[nodiscard]] T getline_as(std::istream &s);

template <>
[[nodiscard]] std::string getline_as<std::string>(std::istream &s)
{
    std::string str;
    std::getline(s, str);
    return str;
} 

template <typename T>
[[nodiscard]] T getline_as(std::istream &s)
{
    while (true)
    {
        T value{};
        std::istringstream convert(getline_as<std::string>(s));
        if (convert >> value)
            return value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++将自定义类型的转换运算符重载为std :: string - C++ overloading conversion operator for custom type to std::string 自动类型转换为std :: string和char *之间的C ++差异 - C++ difference between automatic type conversion to std::string and char* 从std :: string获取类型,C ++ - Get type from std::string, C++ C ++为什么要使用从(std :: string)到(void)类型的隐式转换? - C++ Why use an implicit conversion from (std::string) to (void) type? C ++仅在模板为字符串类型时才执行toLowercase转换 - C++ Only execute toLowercase conversion when template is of type string 如何在C ++中从枚举转换为类型(并在模板中使用)? - How to do a conversion from enum to type (and use as it in a template) in C++? C ++类型转换:错误C2440:“正在初始化”:无法从“ HRESULT”转换为“ std :: basic_string &lt;_Elem,_Traits,_Alloc&gt;” - c++ type conversion: error C2440: 'initializing' : cannot convert from 'HRESULT' to 'std::basic_string<_Elem,_Traits,_Alloc>' c ++模板中的隐式类型转换 - Implicit type conversion in c++ template C ++类模板类型std :: list - C++ Class template type std::list C ++&#39;==&#39;:没有从&#39;int&#39;到&#39;std :: string&#39;的转换 - C++ '==': no conversion from 'int' to 'std::string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM