implicit-conversion

What's the difference between integer promotions and integer conversions in C++

梦想的初衷 提交于 2019-12-21 16:55:23
问题 Section 4.5 of the C++ standard (integer promotion) talks about specific cases of converting integral types to types with a higher rank. Section 4.7 of the C++ standard (integral conversions) begins with (bullet 4.7.1): An rvalue of an integer type can be converted to an rvalue of another integer type. An rvalue of an enumeration type can be converted to an rvalue of an integer type. As far as I understand conversions described in 4.5 (maybe except for the bullet 4.5.3 (enums)) can be

Implicit conversion of a function to a second-order-function only works if the function to convert has at least two parameters

。_饼干妹妹 提交于 2019-12-21 12:12:53
问题 I have a problem of implicit conversions and higher-order functions. It seems that an implicit conversions of a function to a second-order-function only works, if the function to convert has at least two parameters. Works: implicit def conv(foo: Integer => String): String => String = null Does not work: implicit def conv(foo: Integer => String): String => String => String = null Works: implicit def conv(foo: (Integer, Integer) => String): String => String => String = null Full example with

Since there are two ways to define a conversion in C++ how do they interact when there are two possibilities for the same conversion?

不想你离开。 提交于 2019-12-21 12:08:30
问题 I am just looking for clarification on how C++ works, this isn't really about solving a particular problem in my code. In C++ you can say that type A should implicitly convert to type B in two different ways. If you are the author of A you can add something like this to A: operator B() { // code } If you are the author of B you can add something like this to B: B(const A &a) { // code } Either one of these, if I understand correctly, will allow A to implicitly convert to B. So if both are

spray-json error: could not find implicit value for parameter um

China☆狼群 提交于 2019-12-21 07:34:22
问题 I have this case class case class Person(val name: String) object JsonImplicits extends DefaultJsonProtocol { implicit val impPerson = jsonFormat1(Person) } I'm trying spray-json in order to parse post request: post { entity(as[Person]) { person => complete(person) } } However I get when I try to compile this: src/main/scala/com/example/ServiceActor.scala:61: error: could not find implicit value for parameter um: spray.httpx.unmarshalling.FromRequestUnmarshaller[com.example.Person] I don't

Why can a string literal be implicitly converted to char* only in certain case? [duplicate]

霸气de小男生 提交于 2019-12-21 07:25:31
问题 This question already has answers here : Why is passing a string literal into a char* argument only sometimes a compiler error? (6 answers) Closed 6 years ago . void f(char* p) {} int main() { f("Hello"); // OK auto p = "Hello"; f(p); // error C2664: 'void f(char *)' : cannot convert parameter 1 // from 'const char *' to 'char *' } The code was compiled with VC++ Nov 2012 CTP. §2.14.15 String Literals, Section 7 A narrow string literal has type “array of n const char”, where n is the size of

delete cout; delete cin; do not give compilation error - a flaw in the Standard library?

北慕城南 提交于 2019-12-21 03:35:46
问题 Will the following give a compilation error? delete cout; delete cin; The answer is : No. It is a flaw in the implementation of stream classes from the Standard library. They have the following conversion function to void* type, which means, all stream objects can be implicitly converted to void* : operator void * ( ) const; This is very useful in general as it lets us write very idiomatic loop, say, when reading input from files. But at the same time, it lets user to write delete stream . As

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

独自空忆成欢 提交于 2019-12-21 03:35:14
问题 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

Question regarding implicit conversions in the C# language specification

孤街浪徒 提交于 2019-12-20 19:25:10
问题 Section 6.1 Implicit conversions defines an identity conversion thusly: An identity conversion converts from any type to the same type. This conversion exists such that an entity that already has a required type can be said to be convertible to that type. Now, what is the purpose of sentences such as these? (In §6.1.6 Implicit reference conversions) The implicit reference conversions are: [...] From any reference-type to a reference-type T if it has an implicit identity or reference

Question regarding implicit conversions in the C# language specification

余生颓废 提交于 2019-12-20 19:24:03
问题 Section 6.1 Implicit conversions defines an identity conversion thusly: An identity conversion converts from any type to the same type. This conversion exists such that an entity that already has a required type can be said to be convertible to that type. Now, what is the purpose of sentences such as these? (In §6.1.6 Implicit reference conversions) The implicit reference conversions are: [...] From any reference-type to a reference-type T if it has an implicit identity or reference

Unclear use of operator double()

你。 提交于 2019-12-20 17:59:52
问题 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; double getArea() {return _x * _y;} }; int main() { Rectangle r(3, 2.5); cout << r << endl; return 0; } I don’t understand why operator double() is invoked, rather than operator std::string() . As far as I know, according to C++ wikibook, operator double is used to