compile-time

Compile-time sizeof conditional

我怕爱的太早我们不能终老 提交于 2019-12-10 01:30:39
问题 I want to define a macro if a condition involving sizeof is true and do nothing (but still compile) if it is false. If the preprocessor supported sizeof , it would look like this: #if (sizeof(void*) <= sizeof(unsigned int)) // what goes here? # define POINTER_FITS_INTO_UINT #endif There are some pages (e.g. http://scaryreasoner.wordpress.com/2009/02/28/checking-sizeof-at-compile-time/) which explain how to make a compile-time assertion on sizeof (and fail to compile if it fails), but I don't

Compile time vs run time errors [duplicate]

喜欢而已 提交于 2019-12-09 18:46:00
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Runtime vs Compile time How should I know whether a specific line of code in Java may throw a compile time or run-time error? Assuming that the specific line of code anyway throws and error. 回答1: In Eclipse, compile time errors will be underlined in red. A compile time error is an error that is detected by the compiler. Common causes for compile time errors include: Syntax errors such as missing semi-colon or

C# Compile-Time Concatenation For String Constants

萝らか妹 提交于 2019-12-09 15:53:22
问题 Does C# do any compile-time optimization for constant string concatenation? If so, how must my code by written to take advantage of this? Example: How do these compare at run time? Console.WriteLine("ABC" + "DEF"); const string s1 = "ABC"; Console.WriteLine(s1 + "DEF"); const string s1 = "ABC"; const string s2 = s1 + "DEF"; Console.WriteLine(s2); 回答1: Yes, it does. You can verify this using by using ildasm or Reflector to inspect the code. static void Main(string[] args) { string s = "A" + "B

Compile-time population of data structures other than arrays?

给你一囗甜甜゛ 提交于 2019-12-09 14:20:07
问题 In C++, you can do this: static const char * [4] = { "One fish", "Two fish", "Red fish", "Blue fish" }; ... and that gives you a nice read-only array data-structure that doesn't take any CPU cycles to initialize at runtime, because all the data has been laid out for you (in the executable's read-only memory pages) by the compiler. But what if I'd rather be using a different data structure instead of an array? For example, if I wanted my data structure to have fast lookups via a key, I'd have

Compile time recursion and conditionals

放肆的年华 提交于 2019-12-08 16:01:20
问题 I was reading the responses to "Printing 1 to 1000 without loop or conditionals" and I am wondering why it is necessary to have the special case for NumberGeneration<1> in the top answer. If I remove that and add a check for N == 1 in the template (code below), the code fails compilation with "template instantiation depth exceeds maximum" but I'm not sure why. Are conditionals handled differently in compile-time? #include <iostream> template<int N> struct NumberGeneration { static void out

Is Polymorphism a waste to apply for the classes that we exactly know the type prior run-time?

偶尔善良 提交于 2019-12-08 12:33:26
问题 Run-time Polymorphism can be used to let the run-time to dynamically load the exact concrete class of an abstract class/interface. (You can take Animal/Dog, Vehicle/Car examples) But when we know the exact concrete class @coding-time (compile-time), does it really need to forcefully apply polymorphism? 回答1: When I write OO code, I tend to use most-general type I can on the left-hand side of the assignment. This immediately means that my answer to your question is - no. Here's the example:

How to get a compile time error instead of NoSuchMethodError run-time error for android API level inconsistencies?

ε祈祈猫儿з 提交于 2019-12-08 07:50:57
问题 I have an Android Xamarin appliation with android:minSdkVersion="15" and android:targetSdkVersion="21" . Turned out that IDE (xamarin studio, visual studio) and compilation process just ignore if I'm using something above API 15 which is not expected behavior here. For example there is a method Path.addArc(float,float,float,float,float,float) from API 21 and I was able to use it with the manifest settings above which gave me a run-time error NoSuchMethodError . There is an easy fix to use an

Assigning unique integral identifier for types, compile-time

霸气de小男生 提交于 2019-12-08 06:12:58
问题 I wonder whether some template meta-programming facility allows one to assign unique integral identifiers for different types, i.e. something like this: class Type; enum { id = identifier<Type>() /* or identifier<Type>::id, ... */ }; static_assert(id == identifier<Type>(), "..."); The hard part, I think, is that the identifier should remain the same across a single compilation (which is not necessarily the same thing as a compilation unit). But of course, as I don't know the technique or if

How to check, if the class is abstract, at compile time?

浪子不回头ぞ 提交于 2019-12-07 08:27:00
问题 By an abstract class I mean the class with at least one pure virtual method. I want the compilation to fail, if the check shows that the class is not abstract. Is it even possible? 回答1: Use std::is_abstract. #include <type_traits> static_assert(std::is_abstract<T>(), "T ought to be abstract."); See it in action. 来源: https://stackoverflow.com/questions/21369036/how-to-check-if-the-class-is-abstract-at-compile-time

How do you enumerate the names and types inside a struct or class at compile time in D?

空扰寡人 提交于 2019-12-07 05:48:56
问题 How do you enumerate the names and types inside a struct or class at compile time? i.e. to do the following: struct Foo { int x; int y; } string serialise!(A)(A a) { ...magic... } auto f = Foo(1,2); serialise(f); -> "x:1, y:2" Thanks, Chris. 回答1: Like this: foreach (index, field; myStruct.tupleof) { // field.stringof is "field", slice is to cut off "myStruct." pragma(msg, "Name: " ~ myStruct.tupleof[index].stringof[9..$]); pragma(msg, "Type: " ~ typeof(field).stringof); } Practical example: