简体   繁体   中英

How to convert std::string to any of several types in constructor?

I have the following class template which has a member variable whose type is determined by the template argument. I want to initialize the value of this member in the constructor, which only takes a std::string . Thus, my problem is that I need to convert std::string to any of several types ( int , double , bool , string ). I don't think I can specialize just the constructor, and I would rather not specialize the entire class for each type. The problem with my code below is that stringstream stops streaming out when it hits a space:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

template <typename Ty>
struct Test
{
    Ty value;

    Test(string str) {
        stringstream ss;
        ss.str(str);
        ss >> value;
    }
};


int main()
{
    Test<int> t1{"42"};
    Test<double> t2{"3.14159"};
    Test<string> t3{"Hello world"};

    cout << t1.value << endl << t2.value << endl << t3.value << endl;

    return 0;
}

The output of the above code is:

42
3.14159
Hello

instead of "Hello world". Is there some way to get stringstream to not stop at whitespace, or some other device that will do arbitrary conversions like I need?

This works for me. Just declare a special implementation before the generalized implementation:

#include <iostream>
#include <string>
#include <sstream>

template<typename T>
struct Test {
    T value;
    Test(std::string);
};

template<>
inline Test<std::string>::Test(std::string str) {
    value = str;
}

template<typename T>
inline Test<T>::Test(std::string str) {
    std::stringstream ss;
    ss.str(str);
    ss >> value;
}

int main() {
    Test<int> t1{"42"};
    Test<double> t2{"3.14159"};
    Test<std::string> t3{"Hello world"};

    std::cout
        << t1.value << std::endl
        << t2.value << std::endl
        << t3.value << std::endl;

    return 0;
}

Here is an ideone working example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM