Aus*_*yde 3 c++ templates operator-overloading operators
我在几个地方看过这个,为了证实我并不疯狂,我寻找其他的例子.显然,这也可以有其他风格,例如operator+ <>
.
然而,我在任何地方都没有看到它是什么,所以我想我会问.
这不是谷歌最简单的事情operator<< <>(
:-)
Jam*_*lis 13
<>
声明中的函数名称(包括运算符之类operator<<
)后表示它是函数模板特化.例如,使用普通的功能模板:
template <typename T>
void f(T x) { }
template<>
void f<>(int x) { } // specialization for T = int
Run Code Online (Sandbox Code Playgroud)
(请注意,尖括号中可能包含模板参数,具体取决于函数模板的专用方式)
<>
当有一个非模板函数通常在重载决策中更好地匹配时,也可以在函数名称之后使用函数名称来显式调用函数模板:
template <typename T>
void g(T x) { } // (1)
void g(int x) { } // (2)
g(42); // calls (2)
g<>(42); // calls (1)
Run Code Online (Sandbox Code Playgroud)
所以,operator<< <>
不是运营商.