How does the operator overload resolution work within namespaces?

I found a strange behaviour of C++ resolution of operator-overloading, I can't explain myself. A pointer to some resource describing it would be just as nice as an answer. I have 2 translation units. In one (called util.cpp/h) I declare and define two operators (I omit the real implementations for readabilty, the problam occurs anyway): // util.h #ifndef GUARD_UTIL #define GUARD_UTIL #i

运算符重载解析如何在名称空间内工作?

我发现了运算符重载的C ++解析奇怪的行为,我无法解释我自己。 指向某些描述它的资源的指针与答案一样好。 我有2个翻译单元。 在一个(称为util.cpp / h)中,我声明并定义了两个运算符(我省略了可读性的实际实现,无论如何都发生了这个问题): // util.h #ifndef GUARD_UTIL #define GUARD_UTIL #include <iostream> std::istream& operator>>(std::istream& is, const char* str); std::istream&a

Where should I define operator >> for my specialization of std::pair?

Consider the following program: #include <iostream> #include <iterator> #include <vector> #include <utility> using namespace std; //just for convenience, illustration only typedef pair<int, int> point; //this is my specialization of pair. I call it point istream& operator >> (istream & in, point & p) { return in >> p.first >> p.se

我应该在哪里定义运算符>>用于我的std :: pair的专业化?

考虑以下程序: #include <iostream> #include <iterator> #include <vector> #include <utility> using namespace std; //just for convenience, illustration only typedef pair<int, int> point; //this is my specialization of pair. I call it point istream& operator >> (istream & in, point & p) { return in >> p.first >> p.second; } int main

Overloading operators outside of the class

So, i have a simple class: class complex{ private: double a,b; public: void setA(double a){ this->a=a; } void setB(double b){ this->b=b; } double getA(){ return a; } double getB(){ return b; } friend complex operator+(const complex&, const complex&); }; And i have the actual overloaded operator here: complex operator+(const complex& x, const com

在班级以外重载操作员

所以,我有一个简单的课程: class complex{ private: double a,b; public: void setA(double a){ this->a=a; } void setB(double b){ this->b=b; } double getA(){ return a; } double getB(){ return b; } friend complex operator+(const complex&, const complex&); }; 我在这里有实际的重载操作符: complex operator+(const complex& x, const complex& y){

How to overload unary & outside class?

You can overload the unary & operator inside a class as: struct X { void* operator &() { return this; } }; so that it returns an address. How would you overload it outside of a class: struct X { }; void* operator &(const X& x) { //how? } Taking the address of the parameter would result in infinite recursion. In C++11, there is template< class T > T

如何超载一元和外部类?

你可以将一个类中的一元&运算符重载为: struct X { void* operator &() { return this; } }; 以便它返回一个地址。 你如何在课堂之外重载它: struct X { }; void* operator &(const X& x) { //how? } 取参数的地址会导致无限递归。 在C ++ 11中,有template< class T > T* std::addressof(T& arg) 。 的std :: addressof 您也可以从Boost Utility获得C ++ 03的相同

Overload comparison operators for a templated class

I'm having troubles in overloading comparison operators in order to compare two pair struct in such way: typedef pair<string, unsigned int> INDEX; bool operator>(INDEX &v1, INDEX &v2) { if(v1.second == v2.second) //if integer parts are equal { //string that comes earlier in the dictionary should be larger return v1.first < v2.first; } ret

为模板类重载比较运算符

我在重载比较运算符时遇到了麻烦,以便以这种方式比较两个pair结构: typedef pair<string, unsigned int> INDEX; bool operator>(INDEX &v1, INDEX &v2) { if(v1.second == v2.second) //if integer parts are equal { //string that comes earlier in the dictionary should be larger return v1.first < v2.first; } return v1.second > v2.second; } 实际的比较

what is the difference between overloading an operator inside or outside a class?

In C++, i know there are two ways to overload. We can overload it inside (like class a ) or outside (like class b ). But, the question is, is there any difference between these two either in compile time or runtime or not? class a { public: int x; a operator+(a p) // operator is overloaded inside class { a temp; temp.x = x; temp.x = p.x; return temp;

在类内部或外部重载运算符有什么区别?

在C ++中,我知道有两种重载方式。 我们可以在内部(如类a )或外部(如类b )重载它。 但问题是,这两者在编译时或运行时是否有区别? class a { public: int x; a operator+(a p) // operator is overloaded inside class { a temp; temp.x = x; temp.x = p.x; return temp; } }; class b { public: friend b operator+(b, b); int x; }; b operator+(b p1, b p2

Superiority of unnamed namespace over static?

未命名的命名空间如何优于static关键字? You're basically referring to the section $7.3.1.1/2 from the C++ Standard, The use of the static keyword is deprecated when declaring objects in a namespace scope; the unnamed-namespace provides a superior alternative. Unnamed namespace is superior to static keyword, primarily because the keyword static applies only to the variables declarations an

匿名命名空间优于静态?

未命名的命名空间如何优于static关键字? 你基本上是指C ++标准中的$ 7.3.1.1 / 2部分, 在命名空间范围内声明对象时,不推荐使用static关键字; 无名命名空间提供了一个更好的选择。 未命名的命名空间优于静态关键字,主要是因为关键字static仅适用于变量声明和函数,而不适用于用户定义的类型。 以下代码在C ++中有效 //legal code static int sample_function() { /* function body */ } static int sample_va

What is external linkage and internal linkage?

I want to understand the external linkage and internal linkage and their difference. I also want to know the meaning of const variables internally link by default unless otherwise declared as extern . When you write an implementation file ( .cpp , .cxx , etc) your compiler generates a translation unit . This is the object file from your implementation file plus all the headers you #include

什么是外部联系和内部联系?

我想了解外部联系和内部联系及其差异。 我也想知道的意义 const变量默认内部链接,除非另外声明为extern 。 当您编写实现文件( .cpp , .cxx等)时,编译器会生成一个翻译单元 。 这是来自实施文件的目标文件以及#include所有标题。 内部联系是指仅在翻译单位范围内的所有内容。 外部联系是指超出特定翻译单位的东西。 换句话说, 可以通过整个程序 (即所有翻译单元(或目标文件)的组合)访问。 正如dudewat所

What exactly is One Definition Rule in C++?

What exactly does One Definition Rule in C++ say? The only trustworthy occurence I can find is in The C++ Programming Language, 3rd. ed., P. 9.2.3. Is there any official definition of the rule except that? The truth is in the standard (3.2 One definition rule) : No translation unit shall contain more than one definition of any variable, function, class type, enumeration type or template.

C ++中的一个定义规则究竟是什么?

C ++中的一个定义规则是什么意思? 我能找到的唯一值得信赖的事情就是“C ++编程语言”第3期。 编辑,第9.2.3节。 除此之外,是否有规则的任何官方定义? 事实是在标准中(3.2一个定义规则): 任何翻译单元都不得包含任何变量,函数,类类型,枚举类型或模板的多个定义。 [...] 每个程序应该包含该程序中使用的每个非内联函数或对象的一个​​定义; 不需要诊断。 定义可以在程序中显式出现,它可以在标准库或用户定义

Automatic source code replacement

I'm looking for a tool, regex or other magic to do some heavy code replacements. Ideally I would be able to replace all instances of the new operator with a call to a function preserving the arguments. What are my options? Update: Example: ClassA* a = new ClassA<int>(1,2,3,4,new ClassB(1,2),"horrible"); Should transform to: ClassA* a = FUNCTION(ClassA<int>(1,2,3,4,FUNCT

自动源代码替换

我正在寻找一个工具,正则表达式或其他魔术来做一些重码替换。 理想情况下,我可以用保存参数的函数调用来替换new运算符的所有实例。 我有什么选择? 更新: 例: ClassA* a = new ClassA<int>(1,2,3,4,new ClassB(1,2),"horrible"); 应该转化为: ClassA* a = FUNCTION(ClassA<int>(1,2,3,4,FUNCTION(ClassB(1,2)),"horrible")); FUNCTION将执行如下操作: FUNCTION(...) Debug(new __VA_ARGS__, __FIL