FAQ Database Discussion Community


Can an unsigned long become negative when multiplied by a float?

c,implicit-conversion
The title is probably ill-chosen, but I could not find a good one-line summary for this question. My problem is that I cannot make sense of what my compiler is doing, and I wonder whether I found a bug in the compiler... or in my understanding of the C language....

Type to impose required constrains on a double

scala,implicit-conversion
I would like to have a run time check on a Double, without having to scatter the check all over my code. I thought that defining an implicit class would do the job, something on the line: implicit class Probability(val x: Double) { require(x >= 0.0 && x <= 1.0,...

Garbage value in implicit conversion from expression `wchar_t + wchar_t[]` to std::wstring

c++,implicit-conversion,undefined-behavior
I have come across something weird in Visual Studio C++ 2013 Community Edition which is either a compiler bug or I'm writing invalid code that does compile without warnings. Consider the following snippet: #include <string> #include <iostream> int main() { std::wstring text; wchar_t firstChar = 'A'; text = firstChar +...

C++ Ambigous call to constructor

c++,constructor,operator-overloading,implicit-conversion,explicit
I have a class with several constructors and overloaded operators : class Utf8String { public: Utf8String(); explicit Utf8String( const char * sStr ); Utf8String( const char * sStrBeg, const char * sStrEnd ); Utf8String( const char * sStr, uint32_t nCpCount ); explicit Utf8String( const Utf8String & sStr ); explicit Utf8String(...

scala implicit convertion and arrow

scala,implicit-conversion
I'm struggling with my DSL building attempt My method should take multiple pairs of (A, B), ideally I'm looking for this kind of code: myMethod( a1 -> b1, a2 -> b2, a3 -> b3) my A and B classes happened to often be constructed with a single string parameter, so...

Does upcasting a null pointer lead to undefined behavior

c++,language-lawyer,implicit-conversion,nullptr,up-casting
I'm wondering whether the following code leads to undefined behavior: #include <cstddef> #include <cstdio> struct IA { virtual ~IA() {} int a = 0; }; struct IB { virtual ~IB() {} int b = 0; }; struct C: IA, IB {}; int main() { C* pc = nullptr; IB* pib...

Can I implement an 'conversion' from string to boolean in C#?

c#,.net,type-conversion,extension-methods,implicit-conversion
There's any way to implement a implicit conversion from 'string' to 'boolean' using C#? E.g. I have the string str with value 'Y' and when I try convert to boolean it must returns me 'true'....

What implicit conversions are supported when using < and > operators?

c#,implicit-conversion
I have got a struct: public struct Decibel { public readonly double Value; public Decibel (double value) { Value = value; } public static implicit operator Decibel (double decibels) { return new Decibel (decibels); } public static implicit operator double (Decibel decibels) { return decibels.Value; } } And now I...

Byte Array to Byte conversion error

c#,bit-manipulation,byte,bytearray,implicit-conversion
I am trying to flip the bits of an unsigned 32-bit integer and output the resultant integer. The following is my code. int numberOfTries = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < numberOfTries; i++) { uint input = Convert.ToUInt32(Console.ReadLine()); byte[] bInput = BitConverter.GetBytes(input); if (BitConverter.IsLittleEndian) Array.Reverse(bInput); byte[] result =...

C++ implicit conversion of pointer type

c++,implicit-conversion,pointer-conversion
Consider this case: int *ptr; int offset; ptr = <some_address>; offset = 10; Assume that offset is 32-bit variable. ptr has type int*, the target architecture is 64-bit (so ptr is 8-byte variable), offset has type int. What conversion will be performed when calculating value of expression*(ptr + offset)? Where...

Scala: generic method using implicit evidence doesn't compile

scala,generics,types,implicit-conversion
I'm learning Scala by working the exercises from the book "Scala for the Impatient". One question asks: Given a mutable Pair[S, T] class, use a type constraint to define a swap method that can be called if the type parameters are the same. My code: class Pair[T, S](var first: T,...

Convert a string to shape [closed]

c#,wpf,implicit-conversion
I'm trying to make a game in C# and for that I need to know how do I change a string to a form, in my case a ellipse. I tried with: int row1 = 1; string var2 = "ell_1_" + row1; Ellipse var1 = Convert.ChangeType(var2, Ellipse); var1.Fill = new...

Python - Can't convert int to str implicity in this specific code [duplicate]

python,implicit-conversion
This question already has an answer here: TypeError: Can't convert 'int' object to str implicitly error python 1 answer I had a GCSE question, where Python is asking me to input three numbers in a function and print the largest one. My problem is, that it shows the error...

Scala Typeclasses with generics

scala,generics,implicit-conversion,typeclass,implicit
I've been playing with the typeclass pattern in Scala, but I haven't been able to figure out how to implement the implicit companion object when the type I'm working with is generic. For example, let's say I've defined a trait for a typeclass that provides functions for putting things into...

Why doesn't std::reference_wrapper implicitly cast to a reference when calling member function?

c++,reference,implicit-conversion
I don't understand exactly why one cannot use a std::reference_wrapper like this: #include <vector> #include <functional> struct Foo { void f() {}; }; int main() { std::vector<std::reference_wrapper<Foo>> vrFoo; Foo foo; vrFoo.push_back(foo); // vrFoo[0].f(); // error vrFoo[0].get().f(); // or static_cast<Foo&>(v[0]).f(); } Why do we have to use the get() member function?...

Implicitly convert groovy type to pass to a fixed non-groovy method

java,scala,groovy,implicit-conversion,polyglot
I am writing a scala application which loads up Groovy "plugin" classes at runtime. Once the plugins are loaded, standard scala types (like List and Option) are passed into them for processing. Groovy naturally doesn't have syntactic sugar for scala types (particularly the Function* family), but I'd like a similar...

What's the appropriate way to prevent a value out of range in an implicit class operator?

delphi,range,implicit-conversion
A small piece of what I'm working on is leading me to create my own record type which requires a particular range of a Double. For example, in the HSV color model, the Hue is represented as a double ranging from 0 to 360. I cannot define this type the...

implicit conversion changes signedness 'int“ to 'unsigned int”

c++,implicit-conversion,memset
I was using clang++ to compile the program and I need to compile it with no error in clang++. I got no error with other compilers. The error line in the code is memset(grid_, 0, sizeof(int) * x_quadrants * y_quadrants); The whole function is like this: Ocean::Ocean(int num_boats, int x_quadrants,...

explain the Outcome of C program [duplicate]

c,implicit-conversion
This question already has an answer here: Why do unsigned int x = -1 and int y = ~0 have the same binary representation? 5 answers I saw following code which prints output "Same" but i am having trouble in understanding this program. Please help me to understand the...

Implicit conversion from lambda expression to user-defined type

c#,lambda,implicit-conversion
I want to define an implicit conversion from (specific) lambda expressions to a user-defined type. I tried the following: public static implicit operator DualElement<T>(Func<OPTatom, OPTatom, T> atomMap) { return new DualElement<T>(e => atomMap(e[0],e[1])); } Then I tried DualElement<double> dubidu = (i, j) => cost[i, j]; which gives "Cannot convert lambda...

How implicit conversion works for non-type template parameters?

c++,templates,type-conversion,implicit-conversion,template-specialization
I guess (certain) implicit conversions apply when passing non-type template parameters. For example, there should be a conversion from int to std::size_t for expressions like std::array<int, 7>. However, consider the following code: template <bool> void f() { std::cout << "false\n"; } template <> void f<true>() { std::cout << "true\n"; }...

Copy constructor used in a “for” loop, but where?

c++,constructor,iterator,const,implicit-conversion
I'm writing an UTF-8 string class and it's two const and non-const iterator classes. I'm encountering a const problem. Here are the classes : class Utf8String { public: class ConstIter; class Iter { friend class ConstIter; private: Iter(); private: Utf8String * m_pStr; utf8::iterator< char * > m_oIter; public: Iter( const...

Finding the implicit value for Json in play

scala,playframework,implicit-conversion
When I use the following code: import JsonImpl.graphFormat val js = Json.toJson(g)(graphFormat) My code compiles and works fine but when I do this it doesn't work and says: "No Json serializer found for type SGraph. Try to implement an implicit Writes or Format for this type." import JsonImpl.graphFormat val js...

Why is implicit conversion not occurring

c++11,implicit-conversion
Why is my conversion constructor not working? The explicit conversion compiles fine. struct element {}; struct element_set { element_set(const element& e) : e(e) {}; element_set& operator+(const element& e) { return *this; } const element &e; }; element e1, e2; auto es1 = element_set(e1) + e2; auto es2 = e1 +...

What does rank mean in relation to type conversion?

c++,type-conversion,implicit-conversion,data-type-conversion,rank
From C++11 standard (draft n3337) §5/9: — If both operands have the same type, no further conversion is needed. — Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type...

Return new something in function returning a shared_ptr

c++,constructor,return,shared-ptr,implicit-conversion
I have a function that returns a shared_ptr to a const object. Returning a shared_ptr constructed from a pointer returned by operator new works, but returning that pointer directly causes compilation error : Error 3 error C2664: 'std::shared_ptr<_Ty>::shared_ptr(std::nullptr_t)' : cannot convert parameter 1 from 'script::float_data *' to 'std::nullptr_t' c:\xxx\value.cpp 59...

Issue with std::reference_wrapper

c++,c++11,language-lawyer,implicit-conversion,reference-wrapper
The issue is clear with the following code: #include <functional> #include <iostream> #include <vector> int main() { //std::vector<int> a, b; int a = 0, b = 0; auto refa = std::ref(a); auto refb = std::ref(b); std::cout << (refa < refb) << '\n'; return 0; } If I use the commented...

NSString stringWithFormat “implicit conversion loses integer precision”

objective-c,nsstring,implicit-conversion,stringwithformat
Im trying to save a high score using HighScore = [[NSUserDefaults standardUserDefaults]integerForKey:@"ScoreSaved"]; Intro3.text = [NSString stringWithFormat:@"HighScore: %i", HighScore]; it says : "implicit conversion loses integer precision, NSInterger(aka long) to int -(void)EndGame { if (ScoreNumber > HighScore){ HighScore = ScoreNumber; [[NSUserDefaults standardUserDefaults]setInteger:HighScore forKey:@"ScoreSaved"]; } this is my first game and i...

Implicit expansion with dependant types

scala,types,implicit-conversion
I wonder why the following case doesn't work. Let's say i have two types: trait Def[T] trait Ref[T] case class Module() And i want to convert a Def to Ref, so i'm declaring the following method: object Ref { implicit def fromDef[A](defin: Def[A])(implicit rc: RefConverter[A]): Ref[rc.ResType] = new Ref[rc.ResType] {}...

Implicit conversion with more than one parameter, and operator overloading

c++,c++11,implicit-conversion
I am currently reading "The C++ Programming Language" book. Below is the relevant code class complex { public: complex(double r, double i): re{r}, im{i} {} complex(double r): complex{r,0} {} complex(): complex{0,0} {} complex& operator-=(complex z) { this->re -= z.re; this->im -= z.im; return *this; } private: double re,im; }; inline...

No viable conversion from std::function to bool

c++,c++11,operator-overloading,implicit-conversion
The C++11 std::function is supposed to implement operator bool() const, so why does clang tell me there is no viable conversion? #include <functional> #include <cstdio> inline double the_answer() { return 42.0; } int main() { std::function<double()> f; bool yes = (f = the_answer); if (yes) printf("The answer is %.2f\n",f()); }...

ScalaXML and empty lines

xml,scala,whitespace,implicit-conversion,removing-whitespace
I'm just getting started in scala and have an issue with representing my domain object (case class) to a well-formatted XML. I have the following implicit converter: implicit def identifierToXml(ident : Identifier): Elem = <identifier> { if(ident.use != null) <use value={ident.use}/> } { if(ident.label != null) <label value={ident.label}/> } {...