Why is there no std::to_string() for string?

Hey guys,

I have a Method that prints a Tree.
I have a Tree where the Keys and Values are std::strings and a Tree where the Keys and Values are int's and i want to print them.

The Print function looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename I, typename T>
void PrintTree(LC::Tree<I, T> tree, std::string prefix)
{
    prefix += "/";
    std::cout << prefix << tree.value() << std::endl;

    for(typename LC::Tree<I, T>::iterator iter = tree.begin(); iter != tree.end(); ++iter)
    {
        std::string tmp = prefix;
        tmp += std::to_string(iter->first);
        PrintTree(iter->second, tmp);
    }
}


and i get the error Message: [...]\main.cpp:61: Error: no matching function for call to 'to_string(const std::basic_string<char>&)'
tmp += std::to_string(iter->first);


I took a look at the C++ reference and yeah, there is no such function.
Why does the standard not provide a simple overloaded method that just returns the parameter? (Java and C# for example provide it and it doesn't hurt)


i wrote a work-around-function but i just wonder why the standard didn't just implement it?

rest of the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <map>
#include <iostream>
#include <string>

template <typename I, typename T>
class Tree : public std::map<I, Tree<I, T>>
{
public:
    Tree() {}
    Tree(T val) { mValue = val; }

    T operator=(const T& rhs) { return mValue = rhs; }
    using std::map<I, Tree<I, T>>::operator=;
    T value() { return mValue; }

private:
    T mValue;
};

template <typename T>
std::string ToString(T val)
{
    return std::to_string(val);
}
template <>
std::string ToString(std::string val)
{
    return val;
}

template <typename I, typename T>
void PrintTree(LC::Tree<I, T> tree, std::string prefix)
{
    prefix += "/";
    std::cout << prefix << tree.value() << std::endl;

    for(typename LC::Tree<I, T>::iterator iter = tree.begin(); iter != tree.end(); ++iter)
    {
        std::string tmp = prefix;
        tmp += ToString(iter->first);
        PrintTree(iter->second, tmp);
    }
}

int main(int argc, char* argv[])
{
    typedef std::string key;
    typedef std::string value;

    LC::Tree<key, value> tree("100");
    tree["0"] = "5";
    tree["0"]["1"] = "10";
    tree["1"] = "15";
    tree["1"]["0"] = "20";
    tree["1"]["0"]["0"] = "25";

    std::map<key, LC::Tree<key, value>> map;
    map = tree;
    tree["2"] = map;
    tree["2"] = tree.value();

    PrintTree(tree, "");

    LC::Tree<int, int> tree2(100);
    tree2[0] = 5;
    tree2[0][0] = 32;

    PrintTree(tree2, "");

    return 0;
}
Last edited on
if you use code block ide, it not support to_string. but dev C++ support it.
TDM-GCC 4.8.1 support it.
I use QtCreator as IDE, to_string is supported.

My question is why there is no
1
2
3
4
std::string to_string(const std::string& str) 
{ 
    return str; 
} 
function in the standard library
http://www.cplusplus.com/reference/string/to_string/

I mean, yeah it is no problem to write that function like this
1
2
3
4
5
6
7
namespace std
{
    std::string to_string(const std::string& str)
    {
	return str;
    }
}
but why didn't they just add it in the library?
Last edited on
std::to_string seems to be only for numerical values. Consider using stream operator<< instead.

1
2
3
std::ostringstream oss(prefix);
oss << iter->first;
PrintTree(iter->second, oss.str());
Last edited on
Okey thanks for the information, so std::to_string really was only supposed for numbers, thanks then!
Topic archived. No new replies allowed.