Template functions for all types except one?

Hey there,

I wanted to overload operator+ for std::string and started like this
1
2
3
4
5
template <typename T>
std::string operator+(const std::string& rStr, const T& rVal)
{ 
    return rStr + std::to_string(rVal);
}

which obviously failed after trying to write this because std::string allready has the operator+(std::string&, std::string&) overloaded:
1
2
3
4
std::string str1("Hello");
str2(" Günther"); 

std::cout << str1 + str2 << std::endl; 


So, I just implemented the function for all types i know:
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
#include <string>
#include <stdint>

std::string operator+(const std::string& rhs, const int8_t lhs)
{
    return rhs + std::to_string(lhs);
}
std::string operator+(const std::string& rhs, const int16_t lhs)
{
    return rhs + std::to_string(lhs);
}
std::string operator+(const std::string& rhs, const int32_t lhs)
{
    return rhs + std::to_string(lhs);
}
std::string operator+(const std::string& rhs, const int64_t lhs)
{
    return rhs + std::to_string(lhs);
}

std::string operator+(const std::string& rhs, const uint8_t lhs)
{
    return rhs + std::to_string(lhs);
}
std::string operator+(const std::string& rhs, const uint16_t lhs)
{
    return rhs + std::to_string(lhs);
}
std::string operator+(const std::string& rhs, const uint32_t lhs)
{
    return rhs + std::to_string(lhs);
}
std::string operator+(const std::string& rhs, const uint64_t lhs)
{
    return rhs + std::to_string(lhs);
}

std::string operator+(const std::string& rhs, const float lhs)
{
    return rhs + std::to_string(lhs);
}
std::string operator+(const std::string& rhs, const double lhs)
{
    return rhs + std::to_string(lhs);
}
std::string operator+(const std::string& rhs, const long double lhs)
{
    return rhs + std::to_string(lhs);
}


Now I want to implement those operators for const char and const char* on the left side as well and after that also change rhs with lhs (so that std::string is on the right side) and then I'd like to implement operator+= as well.

I could go on like this but that wouldn't make much of a pretty code because it would take up about 11 times the space so I'd like to know if it is somehow possible to make a template function for all except one type.
Last edited on
std::enable_if http://www.cplusplus.com/reference/type_traits/enable_if/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

template < class T,
           class = typename std::enable_if<std::is_integral<T>::value>::type>
std::string operator +(std::string& rhs, const T& lhs)
{
	return rhs + std::to_string(lhs);
}

int main() {
	std::string s("test");
	std::cout << s+9333;
}
http://ideone.com/IPw8NN
Thats a very good start, thank you!

So, I have used is_arithmetic instead of is_integral because it should work for floating point variables as well.

But how can I filter out char?

edit: Nevermind, i just addded !(std::is_same<char, T>::value)
1
2
3
4
5
6
template < class T,
		   class = typename std::enable_if<std::is_arithmetic<T>::value && !(std::is_same<char, T>::value)>::type>
std::string operator +(std::string& rhs, const T& lhs)
{
	return rhs + std::to_string(lhs);
}


edit2: Holy shit this is usefull, thanks for showing me naraku9333!!! :D
Last edited on
Topic archived. No new replies allowed.