casting

How can the offset for a JVM jump instruction be 32768?

烈酒焚心 提交于 2019-12-23 08:49:13
问题 While writing an answer to a question about JVM byte code offsets, I noticed something in the behavior of javac and the resulting class files that I can not explain: When compiling a class like this class FarJump { public static void main(String args[]) { call(0, 1); } public static void call(int x, int y) { if (x < y) { y++; y++; // ... (10921 times - too much code to post here!) y++; y++; } System.out.println(y); } } then the resulting byte code will contain the following if_icmpge

Casting c_str() only works for short strings

倾然丶 夕夏残阳落幕 提交于 2019-12-23 08:48:00
问题 I'm using a C library in C++ and wrote a wrapper. At one point I need to convert an std::string to a c-style string. There is a class with a function, which returns a string. Casting the returned string works if the string is short, otherwise not. Here is a simple and reduced example illustrating the issue: #include <iostream> #include <string> class StringBox { public: std::string getString() const { return text_; } StringBox(std::string text) : text_(text){}; private: std::string text_; };

printf too smart casting from char to int?

不想你离开。 提交于 2019-12-23 07:57:24
问题 Why does the following call: printf("%d %d", 'a', 'b'); result in the "correct" 97 98 values? %d indicates the function has to read 4 bytes of data, and printf shouldn't be able to tell the type of the received arguments (besides the format string), so why isn't the printed number |a||b||junk||junk| ? Thanks in advance. 回答1: In this case, the parameters received by printf will be of type int . First of all, anything you pass to printf (except the first parameter) undergoes "default promotions

c++ cast vector<Inherited*> to vector<abstract*>

荒凉一梦 提交于 2019-12-23 07:47:03
问题 class Interface{}; class Foo: public Interface{}; class Bar{ public: vector<Interface*> getStuff(); private: vector<Foo*> stuff; }; How do I implement the function getStuff() ? 回答1: vector<Interface*> result(stuff.begin(), stuff.end()); return result; 回答2: std::vector<Inherited*> and std::vector<abstract*> are different, and pretty much unrelated, types. You cannot cast from one to the other. But you can std::copy or use iterator range constructor as @Grozz says. Edit: Answering your question

How to cast DbSet<T> to List<T>

谁都会走 提交于 2019-12-23 07:45:56
问题 Given the following simplified Entity Framework 6 context, I am trying to populate a List with the entities but having problems with how to cast (I believe) via reflection. public class FooContext : DbContext { public virtual IDbSet<FooClass> Foo { get; set; } //... } public class FooClass { public int Id{ get; set; } public string Name {get; set; } //... } public main() { using (var context = new FooContext()) { var sets = typeof(FooContext).GetProperties().Where(pi => pi.PropertyType

How to control boolean rendering in xslt

跟風遠走 提交于 2019-12-23 07:42:17
问题 To conform with the <boolean> spec of Xml-RPC I need to transform my xs:boolean from true|false to 1|0 . I solved this using xsl:choose <xsl:template match="Foo"> <member> <name>Baz</name> <value> <boolean> <xsl:choose> <xsl:when test=".='true'">1</xsl:when> <xsl:otherwise>0</xsl:otherwise> </xsl:choose> </boolean> </value> </member> </xsl:template> but was wondering if there is a less brittle way of controlling how boolean values are rendered when transformed with xslt 1.0. 回答1: Use : number

Using typecasting to remove gcc compiler warnings

家住魔仙堡 提交于 2019-12-23 07:28:56
问题 I am doing embedded ARM programming with gcc 4.9. I've been using the -Wconversion switch because it's in my company's default dev tool configuration. I'm using the stdint.h types (uint8_t, uint32_t, etc). The compiler creates warnings every time I perform a compound assignment or even simple addition. For example: uint8_t u8 = 0; uint16_t u16; // These cause warnings: u8 += 2; u8 = u16 >> 8; The "common method" to fix this is to use casts, as discussed here and here: u8 = (uint8_t)(u8 + 2);

IEnumerable.Cast() vs casting in IEnumerable.Select()

偶尔善良 提交于 2019-12-23 07:27:32
问题 Suppose I have an IEnumerable<int> and I want these to be converted into their ASCII-equivalent characters. For a single integer, it would just be (char)i , so there's always collection.Select(i => (char)i) , but I thought it would be a tad cleaner to use collection.Cast() . Can anyone explain why I get an InvalidCastException when I use collection.Cast<char>() but not with collection.Select(i => (char)i) ? Edit: Interestingly enough, when I call collection.OfType<char>() I get an empty set.

Downcasting double to float: is overflow behaviour guaranteed?

南楼画角 提交于 2019-12-23 07:22:53
问题 If I try this float f = (float)numeric_limits<double>::infinity(); Or indeed, try to cast anything bigger than float max down to a float, am I guaranteed to end up with infinity? It works on GCC, but is it a standard though? 回答1: float f = (float)numeric_limits<double>::infinity(); This is guaranteed to set f to infinity if your compilation platform offers IEEE 754 arithmetic for floating-point computations (it usually does). Or indeed, try to cast anything bigger than float max down to a

Flow: Create a flow type by extending another type

放肆的年华 提交于 2019-12-23 07:18:09
问题 type someType = { keyOne: string, keyTwo: string, }; type someOtherType = { keyOne: string, keyTwo: string, keyThree: string, }; Both of these types are objects that contain keyOne and keyTwo , the only difference is the latter extends the former with an additional key of keyThree . Rather than writing duplicated code, is it possible to build the someOtherType flow type by extending someType ? In my mind, ES6 object rest/spread comes to mind, but I'm not sure how to accomplish something like