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
64
votes
3 answers

mysql datetime comparison

For example the following query works fine: SELECT * FROM quotes WHERE expires_at <= '2010-10-15 10:00:00'; But this is obviously performing a 'string' comparison - I was wondering if there was a function built in to MySQL that specifically…
MAX POWER
  • 5,213
  • 15
  • 89
  • 141
22
votes
2 answers

Visual Studio 2013 'explicit' keyword bug?

Consider the following program: #include class A { public: A( ) { std::cout << "A()\n"; } A( A& ) = delete; A( int i ) { std::cout << "A( " << i << " )\n"; } explicit operator int( ) { std::cout << "operator int()\n"; return…
KAI42
  • 243
  • 1
  • 7
17
votes
6 answers

Why do switch and if statements behave differently with conversion operators?

Why do switch and if statements behave differently with conversion operators? struct WrapperA { explicit operator bool() { return false; } }; struct WrapperB { explicit operator int() { return 0; } }; int main() { WrapperA…
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
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
13
votes
4 answers

Cannot implicitly convert type 'decimal?' to 'decimal'.

sdr is my sqldatareader and I want to check that the curPrice value which is of type decimal is null. inrec.curPrice = sdr.IsDBNull(7) ? (decimal?)null : sdr.GetDecimal(7); This is the error message I am getting: Cannot implicitly convert type…
user1270384
  • 711
  • 7
  • 24
  • 53
11
votes
1 answer

Why does the cast operator to a private base not get used?

In this code assigning to b1 works, but it won't allow assigning to b2 (with or without the static cast). I was actually trying to solve the opposite problem, public inheritance but not implicitly converting to the base. However the cast operator…
Bas
  • 474
  • 1
  • 4
  • 12
11
votes
2 answers

Why/when is it important to specify an operator as explicit?

I've borrowed the code below from another question (slightly modified), to use in my code: internal class PositiveDouble { private double _value; public PositiveDouble(double val) { if (val < 0) throw new…
kmote
  • 16,095
  • 11
  • 68
  • 91
11
votes
6 answers

When should I define a (explicit or implicit) conversion operator in C#?

A somewhat little-known feature of C# is the possibility to create implicit or explicit user-defined type conversions. I have been writing C# code for 6 years now, and I have never used it. So, I'm afraid I might be missing good opportunities. What…
10
votes
4 answers

Extension method and Explicit casting

I'm using class from some assembly(source code is not available), so it is not possible to change their's code I need to add extension method for explicit cast operator, is there any way to achieve that? (I have tried to add as regular extension…
Artur Keyan
  • 7,643
  • 12
  • 52
  • 65
10
votes
3 answers

Difference between implicit conversion and explicit conversion

Possible Duplicate: Implicit VS Explicit Conversion What is the difference between "implicit conversion" and "explicit conversion"? Is the difference different in Java and C++?
saplingPro
  • 20,769
  • 53
  • 137
  • 195
10
votes
3 answers

In C++, can we use { } for C-Style casting?

While I have been reading about datatype conversion, I saw this example: void intval() { for (char c; cin >> c; ) cout << "the value of '" << c << "' is " << int{c} << '\n'; } I know that we can cast using: int(c) (int)…
Shadi
  • 1,701
  • 2
  • 14
  • 27
9
votes
1 answer

Why is my "explicit operator bool()" not called?

#include using namespace std; struct A { explicit operator bool() const { return true; } operator int() { return 0; } }; int main() { if (A()) { cout << "true" << endl; } …
xmllmx
  • 39,765
  • 26
  • 162
  • 323
9
votes
4 answers

C# Explicit operator and Object

QUESTION Please take a look to the code first. Here is my custom class: public class float2D { public float X { get; private set; } public float Y { get; private set; } public float2D(float x, float y) { this.X = x; …
Soroush Falahati
  • 2,196
  • 1
  • 27
  • 38
8
votes
4 answers

isSet() or operator void*() or explicit opertor bool() or something else?

What is the state of the art about functions to check whether a value is set or not? For example, the below iterator parses cells. Some cells contain a value, other cells are empty. What is the most convenient way? struct iterator { …
oHo
  • 51,447
  • 27
  • 165
  • 200
1
2 3 4 5 6 7 8