Questions tagged [conversion-operator]

A language feature to allow an object to specify how it can be converted to another type, either implicitly to satisfy a type restriction or explicitly.

143 questions
116
votes
8 answers

What is the meaning of "operator bool() const"

For example: operator bool() const { return col != 0; } col is an int. How does operator bool() const work?
Cheng
  • 1,169
  • 2
  • 8
  • 8
81
votes
2 answers

Conversion constructor vs. conversion operator: precedence

Reading some questions here on SO about conversion operators and constructors got me thinking about the interaction between them, namely when there is an 'ambiguous' call. Consider the following code: class A; class B { public: …
GRB
  • 3,982
  • 4
  • 27
  • 21
51
votes
7 answers

How do conversion operators work in C++?

Consider this simple example: template class smartref { public: smartref() : data(new Type) { } operator Type&(){ return *data; } private: Type* data; }; class person { public: void think() { std::cout << "I am…
Khaled Alshaya
  • 94,250
  • 39
  • 176
  • 234
44
votes
5 answers

What is an "operator int" function?

What is the "operator int" function below? What does it do? class INT { int a; public: INT(int ix = 0) { a = ix; } /* Starting here: */ operator int() { return a; } /* End */ INT operator ++(int) { …
ankit
  • 877
  • 3
  • 9
  • 14
34
votes
3 answers

Is this change in overload resolution between Clang 3.5 and 3.6 correct or a bug?

The code below compiles in Visual Studio 2013, gcc 4.8, clang 3.4 and clang 3.5 (Apple LLVM 6.0) but does not compile in clang 3.6 (via Apple LLVM 6.1) The code is a simplified version of a complicated class in our codebase which is the minimum…
31
votes
4 answers

conversion operator as standalone function

Why does C++ require that user-defined conversion operator can only be non-static member? Why is it not allowed to use standalone functions as for other unary operators? Something like this: operator bool (const std::string& s) { return !s.empty();…
shura
  • 694
  • 5
  • 9
29
votes
4 answers

Conversion Operators in C++

Please help me understand how exactly the conversion operators in C++ work. I have a simple example here which I am trying to understand, though it is not very clear how the conversion actually happens by the compiler. class Example{ public: …
Zuzu
  • 3,363
  • 5
  • 27
  • 16
26
votes
1 answer

What is the purpose of `operator auto() = delete` in C++?

A class in C++ can define one or several conversion operators. Some of them can be with auto-deduction of resulting type: operator auto. And all compilers allow the programmer to mark any operator as deleted, and operator auto as well. For concrete…
Fedor
  • 17,146
  • 13
  • 40
  • 131
26
votes
6 answers

C++ Conversion operator for converting to function pointer

I'm been grinding my head against an idea that is simple enough in my head, but I can't figure out how to implement in C++. Normally, I can declare a class with a conversion operator like in this simple example: class Foo { private: int…
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
22
votes
1 answer

Ambiguity involving templated conversion operator and implicit copy constructor

clang and gcc differ in behaviour for the following code: struct foo { foo(int); }; struct waldo { template operator T(); }; int main() { waldo w; foo f{w}; } This code is accepted by clang, with the foo(int)…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
18
votes
1 answer

Preference of conversion operator over copy constructor changes from C++14 to C++17?

I was playing with the following code. #include struct To { To() = default; To(const struct From&) {std::cout << "Constructor called\n";} To(const To&) {std::cout << "Copy constructor called\n";} }; struct From { …
17
votes
6 answers

Why do switch and if statements behave differently with conversion operators?

Why do switch and if statements behave differently with conversion operators? struct WrapperA { explicit operator bool() { return false; } }; struct WrapperB { explicit operator int() { return 0; } }; int main() { WrapperA…
14
votes
1 answer

Overloading conversion function templates

Consider the following: struct X { template operator T(); // #1 template operator T&(); // #2 }; int a = X{}; // error: ambiguous int& b = X{}; // calls #2 int const& c = X{}; // calls #2 The situation for…
Barry
  • 286,269
  • 29
  • 621
  • 977
14
votes
2 answers

Overload resolution with multiple functions and multiple conversion operators

Consider simple code : #include struct A { operator double(){ std::cout<<"Conversion function double chosen."<
13
votes
1 answer

Which of these conversions should be ambiguous?

I have some code like the following: class bar; class foo { public: operator bar() const; }; class bar { public: bar(const foo& foo); }; void baz() { foo f; bar b = f; // [1] const foo f2; bar b2 = f2; // [2] } GCC…
1
2 3
9 10