A string literal when passed to a function will be deduced to be first a const char (&)[] array first, and then if not function is suitable it will decay to a const char*. So I thought that the following would work, however the const char* function is still being called:
#include <iostream>
//
struct MyString
{
MyString() { }
template <typename ...Ts>
MyString(Ts&& ... args)
{
append(std::forward<Ts>(args)...);
}
void append(const char* str)
{
/* NOW STRING LENGTH WILL HAVE TO BE CALLED ON THIS */
std::cout << "append const char* calledn";
}
template <int N>
void append(const char(&arr)[N])
{
/* I KNOW THE SIZE OF THE STRING HERE */
std::cout << "append arr[N] called n";
}
};
int main()
{
MyString s;// { "Hello" };
s.append("Imagine some really long string");
}
I guess this is because non-template functions are preferred first. Can I make it prefer the array one without deleting the const char*?