explicit-conversion

Visual Studio 2013 'explicit' keyword bug?

不问归期 提交于 2019-12-03 14:56:36
问题 Consider the following program: #include <iostream> 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 42; } }; template< typename T = A > void f( T a = A() ) {} int main( void ) { f(); return 0; } Visual Studio 2013 compiles this code and runs, with output A() operator int() A( 42 ) Is this a compiler bug? It looks like the VS compiler doesn't heed the

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

六月ゝ 毕业季﹏ 提交于 2019-12-03 10:39:25
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 ) needs to provide conversion to any possible type in a convenient, safe (no surprises) way that preserves move semantics. I can think of four different ways. struct A { // explicit conversion operators (nice, safe?) template<typename T> explicit operator T&& () &&; template<typename T> explicit operator T& () &; template<typename T> explicit

Visual Studio 2013 'explicit' keyword bug?

心不动则不痛 提交于 2019-12-03 05:39:48
Consider the following program: #include <iostream> 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 42; } }; template< typename T = A > void f( T a = A() ) {} int main( void ) { f(); return 0; } Visual Studio 2013 compiles this code and runs, with output A() operator int() A( 42 ) Is this a compiler bug? It looks like the VS compiler doesn't heed the 'explicit' keyword in this context. From my understanding, VS 2013 wrongly uses operator int() in combination

Dog to Human years and vice versa [closed]

我们两清 提交于 2019-12-02 23:53:12
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . I am trying out this example problem where I have to make two implicit conversion operators to create a Doggy class from a Human and vice versa. The classes need to take into fact that a human year is 7 dog years. I was told that if going from Dog years to human years to make sure the type of age

Dog to Human years and vice versa [closed]

二次信任 提交于 2019-12-02 13:43:21
I am trying out this example problem where I have to make two implicit conversion operators to create a Doggy class from a Human and vice versa. The classes need to take into fact that a human year is 7 dog years. I was told that if going from Dog years to human years to make sure the type of age is still an int (of course) and that it may require some explicit converting. I don't know how to define DogToHuman.Name, DogToHuman.Age, HumanToDog.Name and HumanToDog.Age. I am kind of new to programming so I am not used to this format. Any number to start with would work, Like human age of 25.

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

∥☆過路亽.° 提交于 2019-11-30 18:44:30
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 'decimal?' to 'decimal'. An explicit conversion exists (are you missing a cast?) Where am I going wrong, please someone tell me. either convert curPrice to nullable, or use .Value property of nullable types. If curPrice is a property of a class then public decimal? curPrice { get; set; } decimal? indicates that it's a nullable decimal; you have to use

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

会有一股神秘感。 提交于 2019-11-30 16:53:43
问题 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 'decimal?' to 'decimal'. An explicit conversion exists (are you missing a cast?) Where am I going wrong, please someone tell me. 回答1: either convert curPrice to nullable, or use .Value property of nullable types. If curPrice is a property of a class

Difference between implicit conversion and explicit conversion [duplicate]

本小妞迷上赌 提交于 2019-11-30 07:07:39
Possible Duplicate: Implicit VS Explicit Conversion What is the difference between "implicit conversion" and "explicit conversion"? Is the difference different in Java and C++? An explicit conversion is where you use some syntax to tell the program to do a conversion. For example (in Java): int i = 999999999; byte b = (byte) i; // The type cast causes an explicit conversion b = i; // Compilation error!! No implicit conversion here. An implicit conversion is where the conversion happens without any syntax. For example (in Java): int i = 999999999; float f = i; // An implicit conversion is

mysql datetime comparison

寵の児 提交于 2019-11-29 21:12:57
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 does 'datetime' comparisons. ...this is obviously performing a 'string' comparison No - if the date/time format matches the supported format, MySQL performs implicit conversion to convert the value to a DATETIME, based on the column it is being compared to. Same thing happens with: WHERE int_column = '1' ...where the string value of "1" is converted to an

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

回眸只為那壹抹淺笑 提交于 2019-11-29 04:02:55
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 are legitimate, good uses of user-defined conversions? Do you have examples where they are better than just defining a custom method? -- Turns out, Microsoft has some design guidelines about conversions, the most relevant of which is: Do not provide a conversion operator if such conversion is not clearly expected by the end users. But when is a