模板类和插入提取重载
如何使插入(<<)和/或提取(>>)运算符在模板类中重载而不使其内联。我想要<<或>>运算符作为友元类。 我知道如何使其内联 矩阵类中内联的示例
friend ostream& operator<<(ostream& ostr, const Matrix<T>& inputMatrix)
{
...
// create the ostr
return ostr;
}
,但我希望代码位于模板类定义之外。
g++ 告诉我添加 <>在函数名称之后,所以我这样做了,但是当我尝试实例化 SOMETYPE 类型的矩阵时,它给了我一个错误,它不知道如何提取或插入该类型。
How can I make the insertion (<<) and/or extraction (>>) operator overloaded in a template class WITHOUT making it inline. I would like to have the << or >> operator as a friend class.
I know how to make it inline
example of inline in a matrix class
friend ostream& operator<<(ostream& ostr, const Matrix<T>& inputMatrix)
{
...
// create the ostr
return ostr;
}
but I'd like to have the code outside of the templateclass definition.
g++ told me to add <> after the function name so I did but when I tried to instansiate a matrix of type SOMETYPE it gave me an error that it didn't know how to extract or insert for that type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您确实想在外部定义运算符并仅与与此模板实例化类型一致的运算符实例化,正确的语法是:
在大多数情况下,考虑到您将定义从类中拉出,这是不值得的麻烦仍然需要在标头中提供它以及所需的额外工作。
另请注意,编译器在查找方面存在细微差别。如果函数内联在类定义中,编译器将找不到该函数除非其中一个参数实际上是模板的类型,因此它有效地降低了可见性和数量编译器必须完成的工作(如果模板化运算符<<是在类外部定义的,则编译器将在它找到
的所有位置将其视为重载解析的候选者) a << b
,仅在第二个参数不是test
的所有情况下丢弃它(并且它将在所有错误中将模板化运算符显示为候选者)无法匹配operator<<
的消息,这已经是一个足够长的列表了)。If you really want to define the operator externally and befriend only the operator instantiation that coincides in type with this template instantiation, the correct syntax is:
In most cases it is not worth the hassle to pull the definition out of the class, considering that you still need to provide it in a header and the extra work required.
Also note that there are slight differences for the compiler regarding lookup. In the case where the function is inlined inside the class definition, the compiler will not find that function unless one of the arguments is actually of the type of the template, so it effectively reduces the visibility and the amount of work that the compiler has to do (if the templated
operator<<
is defined outside of the class, the compiler will find it as a candidate for overload resolution in all places where it findsa << b
, only to discard it in all cases where the second argument is not atest<T>
(and it will show the templated operator as a candidate in all error messages where it cannot matchoperator<<
, which is a long enough list already).尝试类似的操作:
翻译单元参考
重新编辑以解决 aschepler 和 drbeas 提出的评论。
Try something like:
Translation unit reference
re-re-edited to addressed the comments made by aschepler and dribeas.
将代码放置在类定义之外的标头中。或者,将其放入
.tcc
文件中并将其包含在标头的底部。Place the code in the header, outside the class definition. Or, place it in a
.tcc
file and include it at the bottom of the header.