C++ overload shift opertor inside namespace

Viewed 64

i wonder, why the following minimal codes doesn't compile.

In the namespace myns I defined Tokens aka std:vector of strings and overrides the shift operator inside this namespace to print Tokens.

First I thougt, if Tokens defined in the namespace myns, then the overloaded shiftoperator will be used from there. But it don't.

How can I fix that problem?

#include <iostream>
#include <string>
#include <vector>

namespace myns{
    using Token = std::string;
    using Tokens = std::vector<Token>;

    template<typename T>
    T& operator<<(T& t, const Tokens& tokens) {
        for ( const auto& token : tokens) {
            t << '[' << token << ']';
        }
        return t;
    }
}

int main() {

    myns::Tokens tokens {"sin", "(", "4"};
    std::cout << tokens << std::endl;
    return 0;
}
0 Answers
Related