Answer the question
In order to leave comments, you need to log in
Return from function type template?
Why doesn't this code compile?
#include <iostream>
#include <string>
template<class T>
T func(T x, unsigned int y) {
if (std::is_same<T, std::string>::value) {
std::string str = x;
for (int i = 0; i < y; ++i)
str += x;
return str;
} else {
return (x * y);
}
}
int main() {
std::string str = "lol";
func(2, 2);
return 0;
}
Answer the question
In order to leave comments, you need to log in
Because here std::string str = x you are assigning an integer to a string.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question