149 questions
Score of 10
1 answer
599 views
Why does a class inherited from lambda type lack conversion to function pointer in Visual C++?
If a lambda is capture-less, its closure type has conversion operator to function pointer. For example,
auto l = []{};
using F = decltype(+l); //pointer to function type
constexpr F p = l;
And if a ...
Score of 2
1 answer
172 views
Using conversion operator with automatic return type deduction from base class
The following not very long program is treated differently by the current compilers:
class A {
protected:
operator auto();
};
template<class>
struct B : A {
using A::operator auto;
};
...
Score of 5
0 answers
105 views
Is the inconsistency between conversions allowed by std::not_fn and std::function constructor intended?
If you have something like this,
struct Foo {
};
constexpr auto pred = [](){
return Foo{};
};
calling std::not_fn(pred) will fail to compile, expectedly.
However, adding an explicit conversion ...
Score of 5
1 answer
213 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::...
Score of 1
0 answers
48 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 ...
Score of 5
0 answers
260 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 ...
Score of 18
1 answer
725 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&...
Score of 1
2 answers
109 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;
...
Score of 8
1 answer
300 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. ...
Score of 1
1 answer
552 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 ...
Score of 5
1 answer
1150 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() { ... ...
Score of 9
1 answer
220 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 ...
Score of 7
1 answer
259 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 {
...
Score of 3
1 answer
244 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 :
...
Score of 0
1 answer
205 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 ...