简体   繁体   English

什么是C ++中的operator << <>?

[英]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+ <> . 显然,这也可以有其他风格,例如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<< <>( :-) 谷歌operator<< <>( :-)并不是最简单的事情

<> after a function name (including an operator, like operator<< ) in a declaration indicates that it is a function template specialization. <>在声明中的函数名称(包括运算符,如operator<< )后表示它是函数模板特化。 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. 因此, operator<< <>不是运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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