Questions tagged [implicit-conversion]

Converting an object, variable or value from one type to another to satisfy a type restriction, without specifically requesting that conversion through language syntax.

Converting an object, variable or value from one type to another to satisfy a type restriction, without specifically requesting that conversion through language syntax.

Implicit conversion is a subset of .

2401 questions
17
votes
5 answers

C# enum to string auto-conversion?

Is it possible to have the compiler automatically convert my Enum values to strings so I can avoid explicitly calling the ToString method every time. Here's an example of what I'd like to do: enum Rank { A, B, C } Rank myRank = Rank.A; string…
dcompiled
  • 4,762
  • 6
  • 33
  • 36
17
votes
3 answers

Are implicit conversions good or bad in modern C++?

In this proposal: N3830 Scoped Resource - Generic RAII Wrapper for the Standard Library a scoped_resource RAII wrapper is presented. On page 4, there is some code like this: auto hFile = std::make_scoped_resource( ... ); ... //…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
17
votes
2 answers

Multiple implicit conversions on custom types not allowed?

class C { public: C() { } }; class B { public: B(C c) { } B() { } }; class A { public: A(bool b) { } A(B b) { } }; int main() { A a1 = true; // bool -> A is allowed A a2 = B(); // B -> A is allowed …
Nelson Tatius
  • 7,693
  • 8
  • 47
  • 70
17
votes
3 answers

Is there anyway to use C# implicit operators from F#?

If I have a C# class with implicit conversion to double, like so: public class Parameter { private double _value; public Parameter(double value) { _value = value } public static implicit operator double(Parameter p) { return _value;…
Benjol
  • 63,995
  • 54
  • 186
  • 268
16
votes
2 answers

Why is implicit conversion not ambiguous for non-primitive types?

Given a simple class template with multiple implicit conversion functions (non-explicit constructor and conversion operator), as in the following example: template class Foo { private: T m_value; public: Foo(); Foo(const T&…
Cybran
  • 2,203
  • 2
  • 17
  • 34
16
votes
3 answers

Less than operator through implicit conversion?

Consider the following class: struct C { /* Class contents, without any arithmetic operator... */ constexpr operator int() noexcept; // Implicit conversion to int }; My question is: Is C usable in standard algorithms like std::sort that…
Vincent
  • 57,703
  • 61
  • 205
  • 388
16
votes
3 answers

Unclear use of operator double()

I have a Rectangle class with conversion operators to both double and std::string: class Rectangle { public: Rectangle(double x, double y) : _x(x), _y(y) {} operator std::string (); operator double (); private: double _x, _y; …
Alex Goft
  • 1,114
  • 1
  • 11
  • 23
16
votes
2 answers

Implicit conversion from lambda expression to user-defined type

I want to define an implicit conversion from (specific) lambda expressions to a user-defined type. I tried the following: public static implicit operator DualElement(Func atomMap) { return new DualElement(e =>…
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
16
votes
1 answer

Generic conversion operator templates and move semantics: any universal solution?

This is a follow-up of Explicit ref-qualified conversion operator templates in action. I have experimented with many different options and I am giving some results here in an attempt to see if there is any solution eventually. Say a class (e.g. any)…
iavr
  • 7,547
  • 1
  • 18
  • 53
16
votes
4 answers

Should implicit operators handle null?

We've got a type which has an implicit string operator. It looks like this: public class Foo { readonly string _value; Foo(string value) { _value = value; } public static implicit operator string(Foo foo) { …
Steve Dunn
  • 21,044
  • 11
  • 62
  • 87
16
votes
4 answers

Why can't nullptr convert to int?

Summary: nullptr converts to bool, and bool converts to int, so why doesn't nullptr convert to int? This code is okay: void f(bool); f(nullptr); // fine, nullptr converts to bool And this is okay: bool b; int i(b); // fine, bool…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
15
votes
5 answers

How do I perform explicit operation casting from reflection?

I want to use reflection and do either an implicit or explicit coversion using reflection. Given I have defined Foo this way public class Foo { public static explicit operator decimal(Foo foo) { return foo.Value; } public…
David Basarab
  • 72,212
  • 42
  • 129
  • 156
15
votes
5 answers

Type-proofing primitive .NET value types via custom structs: Is it worth the effort?

I'm toying with the idea of making primitive .NET value types more type-safe and more "self-documenting" by wrapping them in custom structs. However, I'm wondering if it's actually ever worth the effort in real-world software. (That "effort" can be…
15
votes
4 answers

Implicit Conversion over a Collection

I ran into a problem this week regarding implicit conversions in C# on collections. While this (using implicit) may not be our final approach, I wanted to at least finish out the code to offer the team as an option. I boiled down the problem to the…
SethO
  • 2,703
  • 5
  • 28
  • 38
15
votes
1 answer

C++17: explicit conversion function vs explicit constructor + implicit conversions - have the rules changed?

Clang 6, clang 7, and gcc 7.1, 7.2, and 7.3 all agree that the following is valid C++17 code, but is ambiguous under C++14 and C++11. MSVC 2015 and 2017 accept it as well. However, gcc-8.1 and 8.2 reject it even in c++17 mode: struct Foo { …
wolfgang
  • 4,883
  • 22
  • 27