implicit-conversion

Is it possible to invoke a user-defined conversion function via list-initialization?

女生的网名这么多〃 提交于 2019-12-28 04:20:55
问题 Is this program legal? struct X { X(const X &); }; struct Y { operator X() const; }; int main() { X{Y{}}; // ?? error } After n2672, and as amended by defect 978, 13.3.3.1 [over.best.ics] has: 4 - However, when considering the argument of a constructor or user-defined conversion function that is a candidate [...] by 13.3.1.7 [...] when the initializer list has exactly one element and a conversion to some class X or reference to (possibly cv-qualified) X is considered for the first parameter

Can we define implicit conversions of enums in c#?

不想你离开。 提交于 2019-12-28 03:19:33
问题 Is it possible to define an implicit conversion of enums in c#? something that could achieve this? public enum MyEnum { one = 1, two = 2 } MyEnum number = MyEnum.one; long i = number; If not, why not? For further discussion and ideas on this, I followed up with how I currently handle this: Improving the C# enum 回答1: There is a solution. Consider the following: public sealed class AccountStatus { public static readonly AccountStatus Open = new AccountStatus(1); public static readonly

Why passing char as parameter to islower() does not work correctly?

白昼怎懂夜的黑 提交于 2019-12-26 15:28:31
问题 My setup: glibc 2.24, gcc 6.2.0, UTF-8 environment. When we call islower() in the following test.c , the result is printed correctly: #include <locale.h> #include <ctype.h> #include <stdio.h> int main (void) { setlocale(LC_ALL, "fr_FR.ISO-8859-1"); if (islower(0xff)) return 0; return 1; } $ gcc -g -o test test.c $ ./test; echo $? 0 Now we change 0xff to 'ÿ' , and get test2.c : #include <locale.h> #include <ctype.h> #include <stdio.h> int main (void) { setlocale(LC_ALL, "fr_FR.ISO-8859-1"); if

Having trouble with implicit conversion in scala

限于喜欢 提交于 2019-12-25 03:22:42
问题 Having this code case class Workspace(ident: Long, name: String) case class Project(ident: Long, name: String) implicit def workspaceJSON: JSONR[Workspace] = new JSONR[Workspace] { def read(json: JValue) = Workspace.applyJSON(field[Long]("id"), field[String]("name"))(json) } implicit def projectJSON: JSONR[Project] = new JSONR[Project] { def read(json: JValue) = Project.applyJSON(field[Long]("id"), field[String]("name"))(json) } def parseEnt[T: JSONR](json: JValue): Either[String, T] =

Converting between types

落花浮王杯 提交于 2019-12-24 19:34:15
问题 I'm studying for the 70-536 exam and now I'm checking the lesson about converting between types and I have doubts. Always implicit conversion it's a widening conversion? and Explicit conversion it's a narrowing conversion? Also this is consider an Narrowing conversion? System.Int32 number32 = 25548612 System.Int16 number16 = (System.Int16) number32; 回答1: That narrowing conversions should be explicit, and widening conversions may be implicitly is only a design guideline. It is possible to

Simple way to add implicit type promotion to the std::complex class operators

匆匆过客 提交于 2019-12-24 19:29:16
问题 I would like to allow the implicit conversion when summing two complex numbers std::complex<double> a; std::complex<long double> b; auto c = a+b; I tried to implement what was suggested here C++ implicit type conversion with template I defined a derived class of std::complex and I added the friend function that should allow the implicit conversion. Here is my code: template <typename T> class Complex : private std::complex<T> { friend Complex operator+ (Complex const &lhs, Complex const &rhs)

Why can't a nullable int be implicitly conversion to an int ? Technical reason or design choice?

别等时光非礼了梦想. 提交于 2019-12-24 06:19:28
问题 In C#, there is no implicit conversion from the int? type to the int type. I have defined the following implicit operator namespace System { public partial struct Int32 { public static implicit operator Int32(int? v) { return (Int32)(v ?? 0); } } } Which allows me to compile the following code int? nullableInt = 0; Int32 regularInt = nullableInt; but if I define regularInt as an int instead of Int32 I get the following error Cannot implicitly convert type 'int?' to 'int'. An explicit

Is there a way to implicitly convert an implicit parameter in Scala?

依然范特西╮ 提交于 2019-12-24 03:27:49
问题 Is there a way to make this work (Scala 2.8.1): class A class B def f(implicit b: B) {} implicit val a = new A implicit def aToB(a: A) = new B f(a) // works ok f // error: could not find implicit value for parameter b: B Actually my problem is with Lift's (2.2) dependency injection, i'm trying to convert Vendor[T] to T and implicitly require it in a class constructor without adding imports after each val: object DependencyFactory extends Factory { implicit def vendorToVal[T](vendor: Vendor[T]

Resolving a conversion warning in a compound assignment

Deadly 提交于 2019-12-23 19:46:07
问题 In my code I have a lot of variable <<= 1; sentences where variable is of type uint16_t. The compiler is spitting a warning saying conversion to 'uint16_t' from 'int' may alter its value [-Wconversion] How can I resolve it? I could use a long-form notation like variable = (uint16_t)(variable << 1) - but I'd like to keep the short notation. 回答1: Based upon my reading of the standard, you can not get away from this warning for this reason: uint16_t foo; foo <<= 1; is equivalent to uint16_t foo;

Chaining implicit conversion operators

我只是一个虾纸丫 提交于 2019-12-23 18:35:55
问题 I've got a class which I need to implicitly convert to a few things, with intermediate values, e.g. struct outer { struct inner { operator T() { return T(); } }; operator inner() { return inner(); } }; If I have this structure, is it always valid to do, e.g. void f(T t); outer o; f(o); 回答1: §13.3.3.1.2 [over.ics.user] p1 A user-defined conversion sequence consists of an initial standard conversion sequence followed by a user-defined conversion (12.3) followed by a second standard conversion