4 ms·
Am I going insane or does the following does NOT work(?): <code> template<typename T> T mul(T a, T b) { return ab; } template<typename T> T mul(T a, int b)
by creativeCak3 5y ago
Am I going insane or does the following does NOT work(?):
<code>
template<typename T>
T mul(T a, T b)
{
return ab;
}
template<typename T>
T mul(T a, int b)
{
std::string ret_val{std::move(a)};
a.reserve(a.length() b);
auto start_pos{a.begin()};
auto end_pos{a.end()};
for(int i = 0; i < b; i++)
{
std::copy(start_pos, end_pos, std::back_inserter(a));
}
return ret_val;
}
</code>
I knew it looked odd to me for some reason...I had to re-write it as follows:
<code>
template<typename T>
T mul(T a, T b)
{
return ab;
}
template<typename T>
T mul(T a, int b)
{
std::string ret_val{};
ret_val.reserve(a.length() b);
auto start_pos{a.begin()};
auto end_pos{a.end()};
for(int i = 0; i < b; i++)
{
std::copy(start_pos, end_pos, std::back_inserter(ret_val));
}
return ret_val;
}
</code>
Did I miss something??
- vips7L 5y agoFour spaces before each line will format your code so other people can read it.
- steveklabnik 5y agoYou only need two on Hacker News, not four. https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc
- vips7L 5y agoTIL thanks.