typecast-operator

Conversion operator in swift

梦想与她 提交于 2019-12-30 10:39:08
问题 Is it possible to write custom conversion (casting) operator in swift ? Especially I'm looking for enums conversion, ex: enum MyEnum : Int { case Case1 = 0 case Case2 func __conversion() -> String { // doesn't work since Swift 1.0 switch self { case Case1: return "Case 1" case Case2: return "Case 2" } } } let enumStr: String = MyEnum.Case1 Of course, I can convert to String with explicit method, but I would like to have implicit mechanism. 回答1: Disclaimer/TL;DR! This answer pertains to the

How to save/load Set of Types?

不羁岁月 提交于 2019-12-28 05:58:16
问题 I have this code type TXSample = (xsType1, xsType2, xsType3, xsType4, xsType5, xsType6, xsType6, xsTyp7, xsType8); // up to FXSample30; .. private FXSample = Set of TXSample; .. published property Sample: TXSample read FXSample write FXSample; .. //if Sample has a value of Sample := [xsType2, xsType4, xsType5, xsType6, xsTyp7]; how can i save/load the property of Sample? i would like to save it in the database. is it possible? 回答1: Provided your set will never exceed 32 possibilities ( Ord

Trouble with cast operators converting to Visual Studio 2013

爷,独闯天下 提交于 2019-12-24 09:37:58
问题 I have been using a simple char buffer class for a number of years under Visual Studio 2005 and 2008. I converted to VS 2013 today and I am getting ambiguous conversion errors in different scenarios. I have removed unneeded portions of the code: #include <string> class ACP { public: ACP(const char* const init) : ourStr_(_strdup(init)), size_(strlen(init) + 1) { } virtual ~ACP() { free(ourStr_); } // // casting operators // operator char* () { return ourStr_; } operator const char* const ()

Need clarification on AnyObject in Swift

不想你离开。 提交于 2019-12-22 13:12:52
问题 Before I start, I have already read the Swift documentation. I am still trying to comprehend what AnyObject actually is. Is it a base class for all objects/classes in Swift, as NSObject is in Objective C? If I create an array of type [AnyObject] and populate it with Movie class instances, that would mean that AnyObject is a base class of the Movie class right? let someObjects: [AnyObject] = [ Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"), Movie(name: "Moon", director:

Ambiguity while overloading the cast operator

血红的双手。 提交于 2019-12-11 04:48:47
问题 Consider the sample code below: #include <iostream> using namespace std; class dummy { private: int y; public: dummy(int b = 0) : y(b) { } friend ostream& operator<<(ostream& os, const dummy& obj); }; ostream& operator<<(ostream& os, const dummy& obj) { os << obj.y; return os; } class sample { private: int x; public: sample(int a = 0) : x(a) { } operator dummy() { dummy d(100); return d; } operator int() { return x; } }; int main() { sample ob1(5); dummy d; //d = ob1; //Line1 d = (dummy)ob1;

Conditionally compile a conversion operator based on numeric template parameter

早过忘川 提交于 2019-12-11 01:18:26
问题 I have a template <bool P> class Foo with lots of code in it. I want to be able to convert Foo<true> 's into Foo<false> 's, i.e. have an operator Foo<false>() method. But the compiler doesn't like such a method existing for Foo, it only likes it for Foo<true> , and gives a warning about how the "operator will not be called for implicit or explicit conversions" (GCC 5.4.x) It doesn't seem like I can use SFINAE for this: std::enable_if works with types; and a value-variant I tried out (where

Why does direct list initialization causes ambiguity for type reference cast if cast operators to the type and reference to the type are declared?

孤街醉人 提交于 2019-12-10 02:02:24
问题 The question rose in context of this answer. Consider an example: struct foo { int value; operator int&(){ return value; } operator int(){ return value; } }; int main () { int &a(foo{}); // #1 //int &b{foo{}}; // #2 -- ambiguity int &c = foo{}; // #3 //int &d = {foo{}}; // #4-- ambiguity int &d { a }; // #5 int &e = { a }; // #6 (void)a; (void)c; (void)d; (void)e; } I don't understand why does #2 and #4 cause ambiguity while #1 and #3 does not. So the question is - why does direct list

In TypeScript, How to cast boolean to number, like 0 or 1

拜拜、爱过 提交于 2019-12-10 02:02:01
问题 As we know, the type cast is called assertion type in TypeScript. And the following code section: // the variable will change to true at onetime let isPlay: boolean = false; let actions: string[] = ['stop', 'play']; let action: string = actions[<number> isPlay]; On compiling, it go wrong Error:(56, 35) TS2352: Neither type 'boolean' nor type 'number' is assignable to the other. Then I try to use the any type: let action: string = actions[<number> <any> isPlay]; Also go wrong. How can I

Conversion of entire array from int to double in order to do some aritmetic operations

谁说我不能喝 提交于 2019-12-08 03:45:12
问题 I have this bit of code: var arrayIntegers : [Int] = [] arrayIntegers += 0...33 var arrayDecimal : [Int] = [] arrayDecimal += 0...999 The problem is I can´t convert the values of both arrays to Double. What I want to do is to get a new array (called arrayDoubleComposed) with composite values, taking a value of arrayIntegers as the integer part and then taking another value of arrayDecimal as the floating part. When I try to typecast this: var arrayDoubleComposed : [Double] = []

Conversion of entire array from int to double in order to do some aritmetic operations

不羁的心 提交于 2019-12-07 18:04:27
I have this bit of code: var arrayIntegers : [Int] = [] arrayIntegers += 0...33 var arrayDecimal : [Int] = [] arrayDecimal += 0...999 The problem is I can´t convert the values of both arrays to Double. What I want to do is to get a new array (called arrayDoubleComposed) with composite values, taking a value of arrayIntegers as the integer part and then taking another value of arrayDecimal as the floating part. When I try to typecast this: var arrayDoubleComposed : [Double] = [] arrayDoubleComposed = Double (arrayIntegers[] + (arrayDecimal[])/1000) I´ve got an error. The same if I suppress the