implicit-conversion

Conversion operator in direct-initialization

久未见 提交于 2020-01-03 13:04:57
问题 The C++14 standard (N4296) says in 8.5/17.6.1 If the initialization is direct-initialization [...], constructors are considered. The applicable constructors are enumerated, and the best one is chosen through overload resolution. [...] If no constructor applies, or the overload resolution is ambiguous, the initialization is ill-formed. Therefore in direct-initialization, only constructors are considered - conversion functions are ignored. In the following code there is no applicable

Why does Int not inherit/extend from Ordered[Int]

孤者浪人 提交于 2020-01-03 10:44:53
问题 I have a question on type design. Why does Int not extend the Ordered trait. Isn't Int ordered by nature? Instead, the scala library provides implicit 'orderer' methods which convert Int to Ordered[Int]. What are the design choices being made here? Example taken from the book Programming in Scala def maxListImpParm[T <% Ordered[T]](elements:List[T]):T= ... maxListImpParm(List(1,5,10,3)) // works because of implicit methods 回答1: Because Int (and some other classes inherited from AnyVal) is

C# enum to string auto-conversion?

孤街醉人 提交于 2020-01-03 07:14:08
问题 Is it possible to have the compiler automatically convert my Enum values to strings so I can avoid explicitly calling the ToString method every time. Here's an example of what I'd like to do: enum Rank { A, B, C } Rank myRank = Rank.A; string myString = Rank.A; // Error: Cannot implicitly convert type 'Rank' to 'string' string myString2 = Rank.A.ToString(); // OK: but is extra work 回答1: No. An enum is it's own type, if you want to convert it to something else, you have to do some work.

User-defined implicit conversion of an enum class when calling an overloaded operator fails

∥☆過路亽.° 提交于 2020-01-02 04:03:14
问题 Consider the following example: struct ConvertibleStruct {}; enum class ConvertibleEC {}; struct Target { // Implicit conversion constructors Target(ConvertibleStruct) {} Target(ConvertibleEC) {} }; Target operator~(const Target& t) { return t; } Target anotherFunction(const Target& t) { return t; } int main() { ConvertibleStruct t; ConvertibleEC ec; ~t; // 1. Works finding the operator overloaded above ~ec; // 2. Fails to compile on clang 3.4 and gcc 4.8.2 operator~(ec); // 3. Works finding

c# parameter implicit conversion

℡╲_俬逩灬. 提交于 2020-01-01 12:02:12
问题 Having this code: class Program { static void Main(string[] args) { Check(3); Console.ReadLine(); } static void Check(int i) { Console.WriteLine("I am an int"); } static void Check(long i) { Console.WriteLine("I am a long"); } static void Check(byte i) { Console.WriteLine("I am a byte"); } } Why this code prints "I am an int" and not "I am a long" ? 回答1: Why this code prints "I am an int" and not "I am a long" ? Because the compiler goes through the rules of overload resolution, which are in

using user-defined conversions with implicit conversions in comparisons

血红的双手。 提交于 2020-01-01 08:03:14
问题 I am struggling to understand why the following code does not allow an implicit conversion to occur. #include <string> using namespace std; struct HasConversionToString { HasConversionToString(const string& s_) : s{s_} {} string s; operator const string&() const { return s; } }; int main() { string s{"a"}; HasConversionToString obj{"b"}; return s < obj; } Both clang and gcc fail to find a valid way to compare the two objects with errors along the lines of: clang++ -std=c++14 -Wall -Wextra

using user-defined conversions with implicit conversions in comparisons

白昼怎懂夜的黑 提交于 2020-01-01 08:01:07
问题 I am struggling to understand why the following code does not allow an implicit conversion to occur. #include <string> using namespace std; struct HasConversionToString { HasConversionToString(const string& s_) : s{s_} {} string s; operator const string&() const { return s; } }; int main() { string s{"a"}; HasConversionToString obj{"b"}; return s < obj; } Both clang and gcc fail to find a valid way to compare the two objects with errors along the lines of: clang++ -std=c++14 -Wall -Wextra

How can I determine if an implicit cast exists in C#?

…衆ロ難τιáo~ 提交于 2020-01-01 07:31:08
问题 I have two types, T and U, and I want to know whether an implicit cast operator is defined from T to U. I'm aware of the existence of IsAssignableFrom, and this is not what I'm looking for, as it doesn't deal with implicit casts. A bit of googling led me to this solution, but in the author's own words this is ugly code (it tries to cast implicitly and returns false if there's an exception, true otherwise...) It seems testing for the existence of an op_Implicit method with the correct

Scala implicit conversion scope issues

倖福魔咒の 提交于 2020-01-01 05:24:11
问题 Take this code: class Register(var value:Int = 0) { def getZeroFlag() : Boolean = (value & 0x80) != 0 } object Register { implicit def reg2int(r:Register):Int = r.value implicit def bool2int(b:Boolean):Int = if (b) 1 else 0 } I want to use it like so: val x = register.getZeroFlag + 10 but I am greeted with: type mismatch; found : Boolean required: Int What goes? Do I need to define a implicit taking a function that returns a bool? 回答1: Here's an example demonstrating how to use your implicits

How to write a C++ conversion operator returning reference to array?

依然范特西╮ 提交于 2020-01-01 04:43:05
问题 In C++ one can add implicit-conversion operators in a class or struct. For instance, 3D vector types usually include something like: struct Vector { float x, y, z; operator float * () { return reinterpret_cast<float *>(this); } }; to allow accessing the vector's elements with subscripts, passing to functions that want a pointer, etc. It occurred to me to wonder: can we instead write a conversion operator that returns a reference to array of float, instead of a pointer to float? (This is of