samedi 25 juin 2016

Overloading a function defined in a namespace

Why is the following code illegal?

#include <iostream>
using namespace std;

namespace what {
void print(int count) {
    cout << count << endl;
}
}

void what::print(const string& str) {
    cout << str << endl;
}

int main() {
    what::print(1);
    what::print("aa");

    return 0;
}

The error I get when compiling with clang and -std=c++14 is

error: out-of-line definition of 'print' does not match any declaration in namespace 'what'

I know the fix to the problem but I am wondering why the compiler thinks that I am trying to define the function (print) instead of overload it.

Aucun commentaire:

Enregistrer un commentaire