Skip to main content
The 2024 Developer Survey results are live! See the results

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.

conversion-operator
5 votes
1 answer
154 views

Why does direct initialization use a const lvalue reference qualified conversion function?

I have two classes st and foo: struct st { st() = default; st(const st&) { std::cout << "copy ctor." << std::endl; } st(st&&) { std::...
ValueError's user avatar
1 vote
0 answers
39 views

Template class with bool conversion operator and template specific instantiation does not link

I prefer to separate template declarations and definitions in headers and compilation units, and use template specific instantiation. But in this example, I cannot get the bool conversion operator ...
Tyson Hilmer's user avatar
5 votes
0 answers
194 views

Implicit moving in return statement requiring a conversion operation

In C++20 there is a rule that informally known as "when returning a variable, first try to move, and only then copy". More formally it is [class.copy.elision]/3 An implicitly movable entity ...
Fedor's user avatar
  • 19.3k
18 votes
1 answer
585 views

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

I was playing with the following code. #include <iostream> struct To { To() = default; To(const struct From&) {std::cout << "Constructor called\n";} To(const To&...
Karen Baghdasaryan's user avatar
1 vote
2 answers
92 views

Is the use of conversion operator forbidden for the lhs of user-defined operator= for user-defined types? If so, what part of the standars forbids it?

Take a simple class wrapping an int, struct Foo { int x; } f; and a class that holds a Foo and that can be converted to it, struct Bar { Foo f; operator Foo&() { return f; ...
Enlico's user avatar
  • 26.7k
8 votes
1 answer
239 views

Prefer one type convert into another through implicit constructor or implicit conversion operator?

Assume we have procedure void f(X2);. Further assume we have types X1 and X2 that do not share an inheritance hierarchy. We want to call it like this: f(X1{}) and have X1 implicitly convert into X2. ...
Post Self's user avatar
  • 1,558
1 vote
1 answer
359 views

Conversion operator with ref-qualifers: rvalue ref and const lvalue ref overloads ambiguity

While answering another question, I noticed something peculiar about conversion operators when dealing with ref-qualifiers. Consider the following code: using P = std::unique_ptr<int>; struct A ...
Nelfeal's user avatar
  • 13.2k
5 votes
1 answer
630 views

Creating functions for each variadic template type

I have several functions for a class which do the exact same thing but with a different type. class ExampleWrapper { public: operator T1() { ... } operator T2() { ... } operator T3() { ... ...
Yeladia's user avatar
  • 151
9 votes
1 answer
183 views

Overload resolution between conversion operators to value and to const-reference in C++

In the following program struct B defines two conversion operators: to A and to const A&. Then A-object is created from B-object: struct A {}; struct B { A a; B() = default; operator const ...
Fedor's user avatar
  • 19.3k
7 votes
1 answer
207 views

Overloaded function and multiple conversion operators ambiguity in C++, compilers disagree

In the following program struct S provides two conversion operators: in double and in long long int. Then an object of type S is passed to a function f, overloaded for float and double: struct S { ...
Fedor's user avatar
  • 19.3k
3 votes
1 answer
202 views

Why is the converting constructor preferred to the conversion operator?

I have this class SmallInt that should represent a positive integer value in the range 0-255-inclusive: struct SmallInt{ explicit SmallInt(int x = 0) : iVal_( !(x < 0 || x > 255) ? x : ...
Itachi Uchiwa's user avatar
0 votes
1 answer
146 views

conversion operator overload : gcc vs clang problem

I am trying to write a basic std::any alternative to use in my code, the reason is that i want to replace the templated std::any_cast with a conversion operator. the use case is to add some ...
user17071443's user avatar
1 vote
2 answers
166 views

C++ use class with conversion operator as index into array

#include <cinttypes> #include <type_traits> template<typename Id, typename Value> class sparse_set { static_assert(std::is_integral_v<Id>, ""); (1) ...
Symlink's user avatar
  • 425
27 votes
1 answer
765 views

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 ...
Fedor's user avatar
  • 19.3k
1 vote
1 answer
247 views

Conversion functions, std::is_base_of and spurious incomplete types: substitution failure IS an error

I'm attempting to implement a conversion function operator, and use std::is_base_of to limit the scope of applicability, but I'm running into issues. #include <type_traits> class Spurious; ...
R.M.'s user avatar
  • 3,596

15 30 50 per page
1
2 3 4 5
10