Questions tagged [explicit-conversion]

This tag is about the `Explicit` C++ keyword.

In C++, the compiler is allowed to make one implicit conversion to resolve the parameters to a function. What this means is that the compiler can use single parameter constructors to convert from one type to another in order to get the right type for a parameter. Adding explicit to a one parameter constructor prevents the compiler for doing so.

106 questions
4
votes
4 answers

explicit/implicit type conversion c++

I have a line of code double i = 1 + (long)1.5* 5.0f My question is what is the conversion order and the result? Been searching for examples like this, but to no avail. Any good guides out there that may help me understand it?
user1601401
  • 732
  • 1
  • 12
  • 25
4
votes
2 answers

Multiple system explicit converters are allowed, but mutiple user explicit converters are not. Why?

If I have this code, this will compile and work as it should: class MyNumber // Just a class. { static public explicit operator MyNumber(byte b) { return new MyNumber(); } } Decimal d = new Decimal(); MyNumber c1 =…
3
votes
1 answer

defining operator + for double and string data types in cpp

I am trying to define operator + for string and double using the following function string operator + (const double& b,const string a){ return to_string(b)+a; } When I am doing the following operation, it works well double c = 100.256; string d…
3
votes
1 answer

Behavior when both conversion constructor and operator are present and explicitness is involved

I have a piece of code where I have both conversion constructor and conversion operator. #include struct ClassFloat; struct ClassInt{ int value; ClassInt(int c) : value(c){std::cout << "From integer\n";}; ClassInt(ClassFloat…
jannarc
  • 93
  • 2
  • 8
3
votes
2 answers

Why Doesn't This Do an Implicit Cast to the Converting Constructor?

So I have this code: struct Foo { Foo() { cout << "default\n"; } Foo(const long long) { cout << "implicit\n"; } }; struct Bar { Bar(const short param) : param(param) {} operator long long() const { return static_cast
3
votes
1 answer

How can convert a List to another List with explicit operator with out using extention method

I add this operator to my class and works well when I pass a class of "A" it convert to class "B". public static explicit operator B (A a) { //Convert A to B } but when I want to convert a list of "A" to a List of "B" it doesn't work. and I try…
Amirhossein Yari
  • 2,054
  • 3
  • 26
  • 38
3
votes
1 answer

Is there a benefit to overloading the explicit operator if implicit has been overloaded in C#?

I am working with a structure that requires the implicit operator against strings and came across a basic question that I had not thought about. public static implicit operator Version (string value) {...} I can understand having only the explicit…
3
votes
2 answers

C# - is operator - Check castability for all conversions available

Edited after reading further, modified question to be more specific. As per Microsoft documentation: An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without…
3
votes
2 answers

Why does C# tease with structural typing when it absolutely knows it doesn't have it?

I was surprised to see today that this was possible, but I worry this must be discussed before. public interface ICanAdd { int Add(int x, int y); } // Note that MyAdder does NOT implement ICanAdd, // but it does define an Add method like the…
nawfal
  • 70,104
  • 56
  • 326
  • 368
3
votes
4 answers

C++ multiple operator=()

I'm writing a String class. I'd like to be able to assign my strings such as; a = "foo"; printf(a); a = "123"; printf(a); int n = a; // notice str -> int conversion a = 456; // notice int -> str conversion printf(a); I've already assigned my…
2
votes
1 answer

explicit template instantiation of explicit operator bool

I'm trying to understand why I get a linker error ( error LNK2001: unresolved external symbol "public: __cdecl Foo::operator bool(void)const_ ) With the following code. If I move the definition of Foo::operator bool() to the header, it builds…
Tyson Hilmer
  • 741
  • 7
  • 25
2
votes
1 answer

C++: Compilation error with explicit keyword

The following code throws compilation error: #include class Option { Option() { printf("Option()\n"); }; public: explicit Option(const Option& other) { printf("Option(const)\n"); *this = other; } …
Saksham Jain
  • 537
  • 3
  • 14
2
votes
0 answers

Explanation for C# Language Specification: 6.2.4 Explicit reference conversions

As I mentioned in this post, I faced a for me not understandable compiler behaviour. The code: IEnumerable> myData = //...getMyData foreach (MyClass o in myData){} It compiles, but fails on runtime: InvalidCastException; for me it is…
Emaborsa
  • 2,360
  • 4
  • 28
  • 50
2
votes
1 answer

compiler failed to apply explicit conversion to an expression used as a condition

Here is my code: #include using namespace std; class Sales{ public: Sales(int i = 0):i(i){} explicit operator int(){ return i; } private: int i; }; int main(){ Sales s(5); if(s == 4) cout << "s == 4" << endl; else…
Yuan Wen
  • 1,583
  • 3
  • 20
  • 38
2
votes
2 answers

Require operator double() to be explicitly called via static_cast(x)

I'd like to enable conversion of my class to a double value. This can be achieved by overloading operator double() but this then allows for implicit conversion, which ideally I'd like to be able to avoid. Is there any way to add this functionality…
Arc
  • 139
  • 6