Hacker News new | ask | show | jobs
by creativeCak3 1762 days 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??

1 comments

Four spaces before each line will format your code so other people can read it.
You only need two on Hacker News, not four. https://news.ycombinator.com/formatdoc
TIL thanks.