简体   繁体   中英

What is operator<< <> in C++?

I have seen this in a few places, and to confirm I wasn't crazy, I looked for other examples . Apparently this can come in other flavors as well, eg operator+ <> .

However, nothing I have seen anywhere mentions what it is, so I thought I'd ask.

It's not the easiest thing to google operator<< <>( :-)

<> after a function name (including an operator, like operator<< ) in a declaration indicates that it is a function template specialization. For example, with an ordinary function template:

template <typename T>
void f(T x) { }

template<>
void f<>(int x) { } // specialization for T = int

(note that the angle brackets might have template arguments listed in them, depending on how the function template is specialized)

<> can also be used after a function name when calling a function to explicitly call a function template when there is a non-template function that would ordinarily be a better match in overload resolution:

template <typename T> 
void g(T x) { }   // (1)

void g(int x) { } // (2)

g(42);   // calls (2)
g<>(42); // calls (1)

So, operator<< <> isn't an operator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM