implicit-conversion

Implicit conversions with std::function [duplicate]

佐手、 提交于 2020-02-26 09:44:07
问题 This question already has answers here : Closed 8 years ago . Possible Duplicates: Why can't my C++ compiler deduce template argument for boost function? Isn't the template argument (the signature) of std::function part of its type? I have the following: #include <functional> void Foo( std::function<void()> ); void Foo( std::function<void(int)> ); void Bar(); int main() { Foo( Bar ); // Error: ambiguous Foo( [](){} ); // Error: ambiguous Foo( std::function<void()>( Bar ) ); // Ok Foo( std:

Why does const allow implicit conversion of references in arguments?

送分小仙女□ 提交于 2020-02-24 09:24:10
问题 This may sound like a silly question, but I was confused about this following behaviour: void funcTakingRef(unsigned int& arg) { std::cout << arg; } void funcTakingByValue(unsigned int arg) { std::cout << arg; } int main() { int a = 7; funcTakingByValue(a); // Works funcTakingRef(a); // A reference of type "unsigned int &" (not const-qualified) // cannot be initialized with a value of type "int" } After thinking about it this kind of makes sense because in passing by value a new variable is

Adding Haskell's Monadic Bind Operator to Scala

两盒软妹~` 提交于 2020-02-03 03:26:08
问题 In Haskell, you can use the bind operator ( >>= ) like this: repli :: [a] -> [a] repli xs = xs >>= \x -> [x,x] *Main> repli [1,2,3] [1,1,2,2,3,3] I've read that flatMap is Scala's bind operator: def repli [A](xs: List[A]): List[A] = xs.flatMap { x => List(x,x) } scala> repli (List(1,2,3)) res0: List[Int] = List(1, 1, 2, 2, 3, 3) As a pedagogic exercise, I'm trying to add support for >>= to Scala: class MyList[T](list: List[T]) { def >>= [U](f: T => List[U]): List[U] = list.flatMap(f) }

Why pointer to function is equal to 1? [duplicate]

南笙酒味 提交于 2020-01-24 12:02:31
问题 This question already has answers here : How to print function pointers with cout? (7 answers) Closed 2 years ago . Check following code: #include <iostream> using namespace std; int& foo() { static int i = 0; return i; } int main() { cout << &foo() << endl; cout << &foo << endl; return 0; } As you see, the first cout prints address of return value of foo() which will be static variable i inside foo() . For 2nd cout I was expecting that &foo returns address of foo() function, as stated here:

Why pointer to function is equal to 1? [duplicate]

吃可爱长大的小学妹 提交于 2020-01-24 12:02:25
问题 This question already has answers here : How to print function pointers with cout? (7 answers) Closed 2 years ago . Check following code: #include <iostream> using namespace std; int& foo() { static int i = 0; return i; } int main() { cout << &foo() << endl; cout << &foo << endl; return 0; } As you see, the first cout prints address of return value of foo() which will be static variable i inside foo() . For 2nd cout I was expecting that &foo returns address of foo() function, as stated here:

In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed?

北战南征 提交于 2020-01-22 06:45:07
问题 The question arose while I was researching the answer to this SO question. Consider the following code: struct A{ operator char() const{ return 'a'; } operator int() const{ return 10; } }; struct B { void operator<< (int) { } }; int main() { A a; B b; b << a; } The conversion of a to int can be either via a.operator char() followed by an integral promotion, or a.operator int() followed by an identity conversion (i.e., no conversion at all). The standard says that (§13.3.3.1 [over.best.ics]

In overload resolution, does selection of a function that uses the ambiguous conversion sequence necessarily result in the call being ill-formed?

爱⌒轻易说出口 提交于 2020-01-22 06:44:45
问题 The question arose while I was researching the answer to this SO question. Consider the following code: struct A{ operator char() const{ return 'a'; } operator int() const{ return 10; } }; struct B { void operator<< (int) { } }; int main() { A a; B b; b << a; } The conversion of a to int can be either via a.operator char() followed by an integral promotion, or a.operator int() followed by an identity conversion (i.e., no conversion at all). The standard says that (§13.3.3.1 [over.best.ics]

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

跟風遠走 提交于 2020-01-13 08:09:52
问题 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 ArgumentOutOfRangeException("Value needs to be positive"); _value = val; } // This conversion is safe, we can make it implicit public static implicit operator double(PositiveDouble d) { return d._value; } // This conversion is not always safe, so we're supposed to make it explicit public

How do I perform explicit operation casting from reflection?

安稳与你 提交于 2020-01-13 08:02:50
问题 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 static explicit operator Foo(decimal number) { return new Foo(number); } public Foo() { } public Foo(decimal number) { Value = number; } public decimal Value { get; set; } public override string ToString() { return Value.ToString(); } } When I run this code decimal

Implicit conversion from array to list

萝らか妹 提交于 2020-01-12 07:34:49
问题 How do I write an implicit conversion from Array[_] to List[_] type? I tried the following but it doesn't seem to work. scala> implicit def arrayToList[A : ClassManifest](a: Array[A]): List[A] = a.toList <console>:5: error: type mismatch; found : Array[A] required: ?{val toList: ?} Note that implicit conversions are not applicable because they are ambiguous: both method arrayToList in object $iw of type [A](a: Array[A])(implicit evidence$1: ClassManifest[A])List[A] and method genericArrayOps