118得票8回答
"operator bool() const"的含义是什么?

例如:operator bool() const { return col != 0; } col 是一个整数。 operator bool() const 如何工作?

82得票2回答
转换构造函数 vs. 转换操作符:优先级

阅读一些关于转换运算符和构造函数的问题,让我思考它们之间的交互,特别是在存在“模糊”调用时。考虑以下代码: class A; class B { public: B(){} B(const A&) //con...

53得票7回答
C++中的转换运算符是如何工作的?

考虑这个简单的例子:template <class Type> class smartref { public: smartref() : data(new Type) { } operator Type&(){ return *data; } privat...

44得票5回答
什么是“operator int”函数?

下面的 "operator int" 函数是什么?它是用来做什么的?class INT { int a; public: INT(int ix = 0) { a = ix; } /* Starting here: */ operator in...

34得票3回答
这个问题是关于Clang 3.5和3.6之间过载解析变化是否正确或者是一个bug。

以下代码可以在Visual Studio 2013,gcc 4.8,clang 3.4和clang 3.5(Apple LLVM 6.0)中编译,但无法在clang 3.6(通过Apple LLVM 6.1)中编译。 这段代码是我们代码库中一个复杂类的简化版本,是展示问题所需的最小要求。 ...

32得票4回答
转换运算符作为独立函数

C++为什么要求用户定义的转换运算符只能是非静态成员函数?为什么不能像其他一元运算符那样使用独立函数呢? 像这样:operator bool (const std::string& s) { return !s.empty(); }

29得票4回答
C++中的转换运算符

请帮我理解 C++ 中的转换运算符是如何工作的。我有一个简单的例子,但编译器实际上是如何进行转换并不是很清楚。class Example{ public: Example(); Example(int val); operator unsigned int(); ...

27得票1回答
`operator auto() = delete` 在C++中的目的是什么?

C++中的类可以定义一个或多个转换运算符。其中一些可以使用自动推导结果类型:operator auto。所有编译器都允许程序员将任何运算符标记为删除,包括operator auto。对于具体类型,删除意味着尝试调用此转换将导致编译错误。但是operator auto() = delete的目的...

26得票6回答
C++ 转换运算符用于将对象转换为函数指针。

我一直在思考一个在我的脑海中非常简单的想法,但是我无法想出如何在C++中实现它。 通常情况下,我可以像这个简单的例子中那样声明一个带有转换运算符的类: class Foo { private: int _i; public: Foo( int i ) : _i(i) ...

22得票1回答
涉及模板转换运算符和隐式复制构造函数的歧义

clang和gcc在以下代码的行为上有所不同: struct foo { foo(int); }; struct waldo { template <typename T> operator T(); }; int main() { waldo...