After C ++ 11, the variadic pattern brought a great new feature to solve such problems. With C ++ 17, the solution got even better.
Here is my template using a folding expression for a palindrome:
template <typename ...ARG> bool isPalindrome(ARG ...args) { std::string temp1 = ""; ((temp1 += args), ...); std::string temp2 = ""; ((temp2 = args + temp2), ...); return temp1 == temp2; } int main(int argc, char *argv[]) { std::cout << isPalindrome('e','y', ' ', 'e', 'd','i','p',' ','a','d','a','n','a','d','a',' ','p','i','d','e',' ','y','e') << std::endl; return 0; }
source share