Const char (&) [N] vs const char * for string literal

foo(const char *)is selected above the template version for "hello". How to make the compiler choose a version of a template?

#include <iostream>

template <size_t N>
void foo(const char (&)[N])
{
    std::cout << N << std::endl;
}

void foo(const char *)
{
    std::cout << "char*" << std::endl;
}

int main()
{
    foo("hello");
    char a[] = "world";
    foo(a);
}
+4
source share

Source: https://habr.com/ru/post/1659641/


All Articles