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
7
votes
2 answers

Why C++ implicit conversion works, but explicit one does not?

The following code compiles successfully in C++11: #include "json.hpp" using json = nlohmann::json ; using namespace std ; int main(){ json js = "asd" ; string s1 = js ; // <---- compiles fine //string s2 = (string)js ; // <---- does…
GetFree
  • 40,278
  • 18
  • 77
  • 104
7
votes
1 answer

Why should I use implicit/explicit operator?

Check the code bellow: class Money { public Money(decimal amount) { Amount = amount; } public decimal Amount { get; set; } public static implicit operator decimal(Money money) { return money.Amount; } …
Marcel James
  • 834
  • 11
  • 20
7
votes
3 answers

Is "if (getline(fin, str)) {}" conforming to the C++11 standard?

I checked the C++11 standard and found the following facts: std::getline(fin, str) returns a basic_ios object, whose class has a member function explicit operator bool() const; The class basic_ios doesn't have a member function operator void*()…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
6
votes
1 answer

Why is my explicit constructor creating this ambiguity for my conversion operator?

I'm unable to figure out why my conversion operator is considering the explicit constructor. #include template struct First { template First(Targs&&... args) {} }; template <> struct…
Richard Fabian
  • 697
  • 1
  • 5
  • 18
6
votes
3 answers

if (static_cast(x)) vs if (x)

I have a coworker who routinely does an explicit cast to bool in conditionals, as in: SomeType *ptr = /* some value */; if (static_cast(ptr)) { // do something } But I haven't been able to find a good reason for such verbosity. Any…
6
votes
1 answer

How to convert primitive type value to enum value, when enum contains elements with the same values?

I wrote the code : enum FlipRotate2dEnum : byte { NO = 0, None = 0, R2 = 1, RotateTwice = 1, FX = 2, FlipX = 2, FY = 3, FlipY = 3, D1 = 4, ReflectDiagonal1 = 4, …
leofun01
  • 125
  • 1
  • 9
6
votes
5 answers

Why can't I implicitly convert a double to an int?

You can implicitly convert an int to a double: double x = 5; You can explicitly convert an int to a double: double x = (double) 5; You can explicitly convert a double to an int: int x = (int) 5.0; Why can't you implicitly convert a double to an…
Evorlor
  • 7,263
  • 17
  • 70
  • 141
5
votes
2 answers

What is the best way to prevent implicit conversion of integer 0 to pointer in c++

I am trying to figure out the best way to prevent integer 0 from being implicitly cast to nullptr_t and then passed to constructors that take pointers. Explicit doesn't do it, but I can get nullptr_t to cause an ambiguous overload error: #include…
5
votes
2 answers

F# and explicit conversion in LINQ to XML

In C# I can express this: var xe = XElement.Parse(""); var maybe = (bool?)xe.Element("bar"); How can this be expressed in F#? Edit: I did find this helper function let inline conv (x : ^a) : ^b = ((^a or ^b) : (static member op_Explicit…
5
votes
2 answers

Why can't coexist implicit and explicit operator of the same type in C#?

Why can not coexist in the same class two operators (explicit and implicit) of the same type? Suppose I have the following: public class Fahrenheit { public float Degrees { get; set; } public Fahrenheit(float degrees) { Degrees…
4
votes
3 answers

explicit cast operator applied to instance created through reflection

I was suprised when found that the following code throws exception at runtime: class A { public string Name { get; set; } public A() { Name = "Class A"; } } class B { public string Name { get; set; } public B() …
Alex
  • 333
  • 2
  • 6
4
votes
2 answers

Are potentially dangerous implicit conversions with std::pair intentional? Is there a way to avoid them?

I just realized this code compiles fine, but has undefined behavior and (naturally) crashes at runtime: #include #include #include int main(int argc, char *argv[]) { std::pair, int> a(&argc, 0); …
user541686
  • 205,094
  • 128
  • 528
  • 886
4
votes
3 answers

Is it possible to overload the “as” or “is” operators

Is this allowed? If not, can this be accomplished inherently by overloading the implicit/explicit conversion operators?
4
votes
1 answer

Priority and ambiguity of explicit conversion operator templates

I've been playing around with templated explicit conversion operators in my project, to implement explicit conversion from custom variant-like type. The minimal example reproducing my problem looks like the following (in C++14 mode): #include…
4
votes
1 answer

Why does this explicit conversion operator work with g++ but not Visual Studio 2013?

The following example contains two templated classes to represent degrees and radians with an explicit conversion operator to cast between them. It compiles and runs with g++ (ideone link) but not with Visual Studio 2013 with Visual C++ Compiler Nov…
x-x
  • 7,287
  • 9
  • 51
  • 78