implicit-conversion

Cannot implicitly convert List<double>' to 'double'

拟墨画扇 提交于 2019-12-31 07:44:07
问题 Keeps throwing, what is wrong in this part of my code, when I want to return cells I receive this error Cannot implicitly convert type 'System.Collections.Generic.List' to 'double' : public double readFileToList(string Path) { var cells = new List<double>(); string path = label3.Text; if (File.Exists(path)) { double temp = 0; cells.AddRange(File.ReadAllLines(path) .Where(line => double.TryParse(line, out temp)) .Select(l => temp) .ToList()); int totalCount = cells.Count(); cellsNo.Text =

Type of char multiply by another char

孤人 提交于 2019-12-31 04:07:47
问题 What is the type of the result of a multiplication of two chars in C/C++? unsigned char a = 70; unsigned char b = 58; cout << a*b << endl; // prints 4060, means no overflow cout << (unsigned int)(unsigned char)(a*b) << endl; // prints 220, means overflow I expect the result of multiplying two number of type T (e.g., char, short, int) becomes T. It seems it is int for char because sizeof(a*b) is 4. I wrote a simple function to check the size of the result of the multiplication: template<class

C# Generic Class Type parameter (cannot implicitly convert)

无人久伴 提交于 2019-12-31 02:49:07
问题 Scenario: class A { } class B : A { } class C<T> where T: A { } Question Why cant C<A> = C<B> when B is a subclass of A? it throws the "cannot implicitly convert" error Thanks --UPDATE-- can i create an implicit method for that C<A> would recognize C<B> ? 回答1: Use co-variant if you need to do this, and because co-variant just work only with interface and delegate , so define an interface with the magic word out instead of class: interface IC<out T> where T : A { } So, you can assign like you

Scala - defining own infix operators

纵饮孤独 提交于 2019-12-31 01:55:11
问题 Methods taking a single argument can be written as an infix operators in Scal. I.e. adding *(other:C) = foo(this, other) to class C, will allow us to write c1 * c2 instead of foo(c1,c2). But is there a way to define infix operators on existing classes that you cannot modify? E.g. if I wanted to write c1 + c2 instead of xor(c1,c2) , where c1,c2:Array[Byte] , I obviously cannot modify the Array-Class. I found this and tried implicit class Bytearray(a1:Array[Byte]) extends Anyval { def +(a2

Can refactoring an overloaded operator into a non-member function break any code?

こ雲淡風輕ζ 提交于 2019-12-30 08:24:24
问题 Consider a legacy class template with overloaded addition operators += and + template<class T> class X { public: X() = default; /* implicict */ X(T v): val(v) {} X<T>& operator+=(X<T> const& rhs) { val += rhs.val; return *this; } X<T> operator+ (X<T> const& rhs) const { return X<T>(*this) += rhs; } private: T val; }; Upon code review, it is observed that + is implementable in terms of += , so why not make it a non-member (and have guaranteed symmetry for left and right arguments)? template

How to convert anything to string implicitly?

六月ゝ 毕业季﹏ 提交于 2019-12-30 07:12:09
问题 My goal is to design a String class that decorates std::string in order to provide some functionality my program needs. One functionality I want to add is the ability to convert anything to my String implicitly in order to save some typing. In order to achieve the implicitly conversion I designed the following class: std::ostream& operator<<(std::ostream& o, const String& s); class String { public: template<typename t_value> String::String(t_value value) { std::ostringstream oss; oss << value

How to convert anything to string implicitly?

六眼飞鱼酱① 提交于 2019-12-30 07:11:45
问题 My goal is to design a String class that decorates std::string in order to provide some functionality my program needs. One functionality I want to add is the ability to convert anything to my String implicitly in order to save some typing. In order to achieve the implicitly conversion I designed the following class: std::ostream& operator<<(std::ostream& o, const String& s); class String { public: template<typename t_value> String::String(t_value value) { std::ostringstream oss; oss << value

Equivalent implicit operators: why are they legal?

老子叫甜甜 提交于 2019-12-30 06:39:11
问题 Update! See my dissection of a portion of the C# spec below; I think I must be missing something, because to me it looks like the behavior I'm describing in this question actually violates the spec. Update 2! OK, upon further reflection, and based on some comments, I think I now understand what's going on. The words "source type" in the spec refer to the type being converted from -- i.e., Type2 in my example below -- which simply means that the compiler is able to narrow the candidates down

Why const for implicit conversion?

拥有回忆 提交于 2019-12-29 04:42:05
问题 After extensive reading of ISO/IEC 14882, Programming language – C++ I'm still unsure why const is needed for implicit conversion to a user-defined type with a single argument constructor like the following #include <iostream> class X { public: X( int value ) { printf("constructor initialized with %i",value); } } void implicit_conversion_func( const X& value ) { //produces "constructor initialized with 99" } int main (int argc, char * const argv[]) { implicit_conversion_func(99); } Starting

Can you use keyword explicit to prevent automatic conversion of method parameters?

浪子不回头ぞ 提交于 2019-12-28 05:01:27
问题 I know you can use C++ keyword 'explicit' for constructors of classes to prevent an automatic conversion of type. Can you use this same command to prevent the conversion of parameters for a class method? I have two class members, one which takes a bool as a param, the other an unsigned int. When I called the function with an int, the compiler converted the param to a bool and called the wrong method. I know eventually I'll replace the bool, but for now don't want to break the other routines