casting

Avoid incompatible pointer warning when dealing with double-indirection

ⅰ亾dé卋堺 提交于 2019-12-23 15:44:42
问题 Assuming this program: #include <stdio.h> #include <string.h> static void ring_pool_alloc(void **p, size_t n) { static unsigned char pool[256], i = 0; *p = &pool[i]; i += n; } int main(void) { char *str; ring_pool_alloc(&str, 7); strcpy(str, "foobar"); printf("%s\n", str); return 0; } ... is it possible to somehow avoid the GCC warning test.c:12: warning: passing argument 1 of ‘ring_pool_alloc’ from incompatible pointer type test.c:4: note: expected ‘void **’ but argument is of type ‘char **’

Unable to use interface with AngularFirebase

独自空忆成欢 提交于 2019-12-23 15:38:24
问题 While trying to create a shopping cart in angular, I've run into type issues regarding my components and services. My service currently returns AngularFireList<{}> and though I can change its type, doing so has side-effects in other methods and inside of my component. I'm wondering if there is an efficient way to allow my current interface to become the type. I've tried to cast the type as Observable<ProdInterface> which again, solves one issue while bringing in another. My current interface

Converting int to a size_t

半腔热情 提交于 2019-12-23 15:26:44
问题 I am wondering about the following warning of the clang compiler when I pass an integer to an std::initializer_list< size_t > : non-constant-expression cannot be narrowed from type 'int' to 'unsigned long' in initializer list Why can int be casted to a size_t but an int not be passed to an std::initializer_list< size_t > , i.e. int main() { size_t s_t = 0; int i = 0; std::initializer_list<size_t> i_l = { i }; // warning s_t = i; // no warning return 0; } 回答1: You have run afoul of [dcl.init

Operator '??' cannot be applied to operands of type for child classes

↘锁芯ラ 提交于 2019-12-23 15:22:45
问题 The following code gives the error in the title on the second line in the Main function. public class P {} public class B : P {} public class A : P {} void Main() { P p = GetA()??GetB(); } public A GetA() { return new A(); } public B GetB() { return new B(); } A simple tweak to the line like these p = (P)GetA()??GetB(); or p = GetA()??(P)GetB(); works. I'm curious why the compiler doesn't understand that both are child classes of the left hand side container and allow the operation without

SmartPointer : cast between base and derived classes

荒凉一梦 提交于 2019-12-23 15:01:26
问题 Say you have a function like this : SmartPtr<A> doSomething(SmartPtr<A> a); And classes like this : class A { } class B : public A { } And now I do this : SmartPtr<A> foo = new B(); doSomething(foo); Now, I would like to get back a SmartPtr<B> object from doSomething . SmartPtr<B> b = doSomething(foo); Is it possible ? What kind of casting do I have to do ? Right now, I just found something I believe ugly : B* b = (B*)doSomething().get() Important notes : I do not have any access to SmartPtr

Casting null to any type

ぃ、小莉子 提交于 2019-12-23 14:20:17
问题 Can null be casted to any type? i.e. will the following code work public <T> T foo(Object object){ return (T) object; } Duck duck = foo(new Duck()); // this works Duck duck2 = foo(null); // should this work? Cat cat = foo(null); // should this work? // if they are both null, should this be true? boolean equality = duck2.equals(cat); My question is, is null 'castable to anything'? 回答1: From Javadocs: There is also a special null type, the type of the expression null, which has no name. Because

Why doesn't returning 0x80000000 from a function that returns int32_t cause a warning?

末鹿安然 提交于 2019-12-23 13:30:27
问题 Consider: int32_t f() { return 0x80000000; } Why doesn't that cause a compiler warning (at least on GCC)? 0x80000000 is out of the range of int32_t ( INT32_MAX is 0x7fffffff ). I believe this should cause an implicit cast - is that correct? Further consder: if (f() == 0x80000000) foo(); The above causes no warning on GCC. However int32 ret = f(); if (ret == 0x80000000) baz(); Causes "warning: comparison between signed and unsigned integer expressions". I believe this is because 0x80000000 has

Can conversion functions be non-member functions

徘徊边缘 提交于 2019-12-23 13:08:55
问题 Is it possible to define the casting operator from one type to another type outside of the class definition as a non-member function? I know it is possible for other operators like operator- but It is not possible with cast operators. For example for two classes A and B, I tried to define the casting operator outside of the A and B scopes as follows: operator A(const B& b) { A a(....); return a; } 回答1: No, conversion functions must be member functions. From C++11, [class.conv.fct]/1: A member

Cannot implicitly convert type 'Program.Data.View' TO System.linq.iqueryable<Program.Data.View>.

a 夏天 提交于 2019-12-23 13:04:07
问题 Goal/Problem: I am trying to use the First or FirstOrDefault to only return 1 result from the Database. I'm getting the following error: Cannot implicitly convert type 'Program.Data.view' to System.Linq.Iqueryable An explicit conversion exists (are you missing a cast) What I've tried: After looking through documentation and many SO articles, I tried different ways of casting, including the code below. Articles such as Cannot implicitly convert type 'System.Linq.IQueryable<>to . An explicit

AS3, loading in a SWF as a custom type

假如想象 提交于 2019-12-23 12:57:25
问题 Fundamental question here. Typically in AS3 you load in a SWF via the Loader, and what you get is some sort of pseudo MovieClip that is of type "Loader". Is there any holy way under the sun to cast this loaded SWF to a custom type that extends MovieClip and not Loader, assuming the SWF was published with a base class of the custom type? Without data loss? Alternatively, let's say you can't, can you even cast it from a custom type that extends Loader itself? 回答1: You can do something like this