2012-03-09 34 views
1

下面的类似乎compile,但转换运营商从来不被称为:超载引用转换

class A 
{ 
public: 
    operator A() const { std::cout << "A() called" << std::endl; return *this; } 
    operator A&() { std::cout << "A&() called" << std::endl; return *this; } 
    operator const A&() const { std::cout << "const A&() called" << std::endl; return *this; } 
}; 

是指定转化为对自身的引用的函数根本不理?

+2

我确定有人会引用这个标准,但同时我会说明一点:这根本没有任何意义。 – David 2012-03-09 04:41:47

回答

1

这里有一个从12.3.2

A conversion function is never used to convert a (possibly cv-qualified) object to the (possibly cv-qualified) same object type (or a reference to it)

而且报价,海合会使用-Wall -Wextra -pedantic -ansi递给我:warning: this statement has no effect的静态铸造。 (另外,尝试clang online它会给你一些不错的编译器错误信息)。