Class variable gets implicitly casted to int despite explicit cast - c++

I wrote implicit cast operator to double and explicit cast operator to int in foo class, and function that takes int as an argument. Despite of being called with not casted foo object code gets executed.
I used gcc PRETTY_FUNCTION and found out that foo object gets casted to double then to int.
class foo
{
public:
int x, y;
foo(int x, int y) : x{x}, y{y} {}
explicit operator int() {cout << __PRETTY_FUNCTION__ << "\n"; return x/y;}
operator double() {cout << __PRETTY_FUNCTION__ << "\n"; return double(x)/double(y);}
};
void printint(int x)
{
cout << x << "\n";
}
int main()
{
foo var(1,2);
printint(var);
}
I expected this not to compile because of explicit operator, but instead it does and prints 0 as int(1/2).
Output of program is:
foo::operator double()
0

From cppreference:
Implicit conversion sequence consists of the following, in this order:
1) zero or one standard conversion sequence;
2) zero or one user-defined conversion;
3) zero or one standard conversion sequence.
Hence your foo can be converted to a double (2) and then the double is converted to an int (3).
If you want the code to not compile you have to also make the double conversion explicit. The explicit only prevents your foo to convert to a int in step 2 alone, but in the whole sequence the conversion can still be done implicitly. Implicit conversion can be tricky and becaue of (1) and (3) a single user provided implicit conversion (2) can have unexpected side effects.

of course it will use the double conversion operator and then cast the double to a int. the double conversion operator can implicitly be called. Remove explicit and it won't cast to double first
It can't use the explicit operator int() because of the explicit

Related

implicit conversions from and to class types

I am studying converting constructors and conversion operators in C++.
What I've learned so far is that any non-explicit constructor that takes only one parameter (and any number of optional default arguments) represents an implicit class-type conversion to THAT class type, for example if a class defines a constructor that has one parameter of type int I can use an int wherever an object of that class type is required:
(assuming class_type has an overloaded += operator)
class_type a;
a+=5;
in this case 5 is implicitly converted (through the converting constructor) to class_typeand the overloaded operator is called.
Now, the (at least for me) tricky part: I know I can define a conversion operator as a member function :
operator int() {....};
that converts the object of class_type to the primitive int type, and I can use that conversion like:
class_type a;
a+5;
in this case I've read that the object is converted to an int through its conversion operator and then the buil-in sum operator is called.
But what if I defined an overloaded + operator to take two class_type objects as its arguments? something like
class_type operator+(const class_type&,const class_type &c);
how is the compiler supposed to know which one to call through function matching?
does the conversion to int only happens implicitly when only the built-in operator is defined?
thanks!
edit:
actually,I've tried to write some code to effectively try it out, it turned out that my compiler (g++) doesn't issue any ambiguous call error!
this is the class header (along with the non-memeber operator+ function declaration) :
#include <iostream>
class wrapper {
friend std::ostream &operator<<(std::ostream&,const wrapper&);
public:
wrapper()=default;
wrapper(int);
int get();
operator int() const;
wrapper operator+(int);
private:
int a=10;
};
std::ostream &operator<<(std::ostream&,const wrapper&);
and this is the main code:
#include "wrapper.h"
int main()
{
using namespace std;
wrapper w1;
wrapper w2(5);
cout<<w1<<" "<<w2<<endl;
w1+1;
}
now,I've defined a converting constructor from int to wrapper AND a conversion operator from class type to int(I've also overloaded the << output operator in order to print some results), but when the compiler evaluates the expression w1+1 it seems to be fine. How could it possibly be??
If you have for example the following class declaration that contains a conversion constructor and a conversion operator
struct A
{
A( int x ) : x( x ) {}
operator int() const { return x; }
int x;
};
const A operator +( const A &a1, const A &a2 )
{
return A( a1.x + a2.x );
}
then statement
a1 + a2;
where a1 and a2 are declared like for example
A a1( 10 );
A a2( 20 );
will be well-formed because there is no need to call a conversion function. The both operands match the parameter declarations of the operator +.
However if you will write for example
a1 + 20;
when the compiler issues an error because there is an ambiguity. The compiler can either apply conversion constructor A( int ) to convert the second operand to type A and call the operator defined for objects of type A. Or it can apply the conversion operator operator int to convert the first operand to type int and call the built-in operator + for objects of type int.
To avoid this ambiguity you could declare either the constructor or the operator (or the both) with function specifier explicit.
For example
explicit A( int x ) : x( x ) {}
or
explicit operator int() const { return x; }
In this case only one implicit conversion would exist and there was not an ambigiuty.
I would like to append the above description that sometimes some converion operators can be called implicitly even if they are declared with the function specifier explicit.
For example According to the C++ Standard (6.4 Selection statements)
...The value of a condition that is an expression is the value of the
expression, contextually converted to bool for statements other
than switch;
and (5.16 Conditional operator)
1 Conditional expressions group right-to-left. The first expression is
contextually converted to bool (Clause 4).
So for example if the above class has the following conversion operator declared with the function specifier explicit
explicit operator bool() const { return x != 0; }
nevertheless it will be called implicitly for example in the following statement
A a( 10 );
std::cout << ( a ? "true" : "false" ) << std::endl;
Here a will be converted to an object of type bool in the conditional operator.
EDIT: After you updated your question this expression
w1+1;
is an exact match for operator
wrapper operator+(int);
Neither conversion are required. So the code compiles successfully.
This is something you can easily try and see what the compiler does:
#include <iostream>
struct ABC {
int v;
ABC(int x) : v(x) { }
operator int() const { return v; }
void operator +=(ABC const &that) {
v += that.v;
}
};
ABC operator+(ABC const &lhs, ABC const &rhs) {
return { lhs.v + rhs.v };
}
int main() {
ABC a(5);
std::cout << a + 1 << '\n';
a += 10;
std::cout << a << '\n';
}
what if I defined an overloaded + operator to take two class_type objects as its arguments?
GCC
error: ambiguous overload for 'operator+' (operand types are 'ABC' and 'int')
The compiler sees two candidates: operator+(int, int) <built-in> and ABC operator+(const ABC&, const ABC&). This means it could implicitly convert not only the 5 in a + 5 to a but also the a to int. Post these conversions both operator+ functions become potential matches.
How is the compiler supposed to know which one to call through function matching?
It doesn't know hence the error.
does the conversion to int only happens implicitly when only the built-in operator is defined?
Yes, otherwise it doesn't automatically convert class_type to int. However, int to class_type would happen implicitly unless you make class_type's constructor explicit:
explicit ABC(int x) : v(x) { }
If you've access to C++11, then you also make the conversion function explicit:
explicit operator int() const { return v; }

Why does float argument fit to int function parameter?

Please, look at this code:
#include <iostream>
class A {
public:
int my;
A(int a=0) : my(a) { }
};
int main() {
A x = 7; // 1
A y = 6.7; // 2
std::cout << x.my << " " << y.my << "\n";
}
It actually compiles although there is no A(double a); constructor.
When exactly compiler is allowed to convert one argument type to another to call corresponding constructor?
cppreference has a list of standard conversions. Of interest to you is the Floating - integral conversions section which can also be found in N4140 4.9/1
A prvalue of floating-point type can be converted to prvalue of any integer type. The fractional part is truncated, that is, the fractional part is discarded.
Finding A(int) to be callable with a standard conversion, the compiler inserts the necessary step to make the code work. It's the same rule that allows int x = 1.1 to compile
If this behavior is undesirable you can forbid it with an =delete
class A {
public:
//...
A(int a);
A(double) =delete;
};

Why is the 'explicit' keyword allowing implicit conversions?

class Test {
private:
int value;
public:
void display(void)
{
cout << "Value [" << value << "]" << endl;
}
explicit Test(int i)
{
value=i;
}
};
int main() {
Test a(5);
Test b(4.9);
a.display();
b.display();
cin.get();
return 0;
}
Float value gets converted to int even though explicit is mentioned.
I was expecting (incorrectly) that float does not get converted to integer and object b not to be constructed.
explicit refers to the constructor itself, not the constructor's parameters. Your explicit constructor may not be used as an implicit conversion to type Test.
void function( Test param );
function( 5 ); // Your "explicit" makes this call an error.
// The parameter must be explicitly cast, such as Test(5)
In C++11 or later, you can prevent implicit parameter conversions using the = delete syntax on a template parameter.
Test(int i)
{
value=i;
}
template<typename T>
Test(const T&) = delete;
// ^ Aside from your int constructor and the implicitly-generated
// copy and move constructors, this will be a better match for any other type
In C++20 or later, you can prevent implicit parameter conversions using the std::same_as concept.
Test(std::same_as<int> auto i)
{
value=i;
}
explicit just prevents any implicit conversions. So if you had:
void foo(Test t);
You cannot call foo(4); because the Test constructor is explicit. You'd have to call foo(Test(4));. The explicit keyword has nothing to do with any conversions that might have to happen during construction.
From the standard [class.conv.ctor]:
An explicit constructor constructs objects just like non-explicit constructors, but does so only where the
direct-initialization syntax (8.5) or where casts (5.2.9, 5.4) are explicitly used.
Which means that Test t = 4; is also illegal, but Test t(42.0) is fine.
It's a Floating–integral conversion.
That is: it's an implicit conversion between a prvalue of type double to a prvalue of type signed int. It discards the fractional part.
TL;DR: the conversion happens between 'double' and 'int', not in your Test constructor. If you want to prevent that constructor to be called with a float or a double you can add the definition:
Test(double) = delete;
In your Test class. Live on compiler explorer

C++ 'overloading' the if() statement

Is it possible to change the behavior of if() so that:
class Foo {
int x;
};
Foo foo;
if(foo)
only proceeds if the value of x is something other than zero? or...
Would an explicit user-defined type conversion to int work/would that be an appropriate approach? or...
Is it best to do something like if(foo.getX())?
You can convert your object to a boolean value by defining operator bool():
explicit operator bool() const
{
return foo.getX();
}
The explicit keyword prevents implicit conversions from Foo to bool. For example, if you accidentally put foo in an arithmetic expression like foo + 1, the compiler could detect this error if you declare operator bool() as explicit, otherwise foo will be converted to bool even if not intended.
In general, member functions of the form
operator TypeName()
(with optional explicit and const qualifier) are conversion operators. It allows you to cast your class to any type specified by TypeName. In the other direction, constructors with one argument allow you to cast any type to your class:
class Foo {
Foo(int x); // convert int to Foo
operator bool() const; // convert Foo to bool
int x;
};
This defines implicit conversions for your class. The compiler tries to apply these conversions if possible (like what it does for built-in data types, e.g. 5 + 1.0). You can declare them to be explicit to suppress unwanted implicit conversions.
You can define an operator to convert the object to bool
class Foo
{
int x;
public:
operator bool() const
{
return x > 0;
}
};
But this can have unintended consequences because of implicit conversions to bool when you don't desire the conversion to take place. For instance
int x = 42 + Foo();
C++11 solves this problem by allowing you to declare the conversion operator as explicit, which then only allows implicit conversions in certain contexts, such as within an if statement.
explicit operator bool() const // allowed in C++11
Now
int x = 42 + Foo(); // error, no implicit conversion to bool
int x = 42 + static_cast<bool>(Foo()); // OK, explicit conversion is allowed

function overload and type conversion resolution

why do we not see a "undefined call to overloaded function" error with the code bellow? just because int is a built in type? where in the standard can I find the guarantee for the conversion to built in type, such as in the code bellow?... thanks!
#include <iostream>
using namespace std;
class B {
public:
operator int(){ return 0; }
};
class A {
public:
A( int i ) { };
};
void f ( int i ) { cout << "overload f(int) was used!";};
void f ( A a ) { cout << "overload f(A) was used!" ;};
int main () {
B b;
f( b );
}
It has nothing to do with being a built-in type. You defined operator int for B. This means that you have provided a user-defined conversion from B to int. According to 12.3.4 of the standard, "at most one user-defined conversion (constructor or conversion function) is implicitly applied to a single value." This is why it is not converted to A, because that would require two implicit conversions.
The rules for determining exactly when this happens are somewhat complicated, so many people advise that you avoid providing user-defined conversions. Another way of defining these is to provide a constructor with one argument; you can add explicit to the beginning to avoid it being applied implicitly.
When you call f(b), the compiler applies the conversion that you provided to convert b to int. If you want to convert it to A, you'll have to define a conversion from B to A, or apply one of the conversions explicitly, like f(int(b)) or f(A(b)).