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
146
questions
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::...
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 ...
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 ...
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&...
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;
...
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. ...
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 ...
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() { ... ...
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 ...
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 {
...
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 :
...
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 ...
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)
...
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 ...
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;
...