mardi 24 octobre 2017

String literal binds to a non-const char pointer

Consider the following program:

#include <iostream>

void myprint(const char* fmt, ...)
{
  std::cout << "1 ";
}

void myprint(const char* fmt, char *x)
{
  std::cout << "2 ";
}

int main()
{
  const char* s = "c";
  myprint("a", s);
  myprint("a", "b");
}

It produces different output:

My question is two-fold:

  1. Why does a string literal bind to a non-const char* even in the presence of -std=c++14? Isn't a string literal const since C++11?

  2. The ellipsis-overload is always ranked lowest. Why does clang select it? (one would think it doesn't allow binding to char* but if I remove the ellipsis overload, it still does - demo)

What's going on and who is right?

Aucun commentaire:

Enregistrer un commentaire