compile-time

constexpr function not calculate value in compile time

橙三吉。 提交于 2019-12-07 02:59:30
问题 I want to compare meta programming and use of constexpr in c++0x. then I write a fib function in both model. when I use meta programming model, answer print out very fast because it calculated in compile time. but when I use constexpr funcion it calculate value in run time, not in compile time. I using g++( gcc ) 4.8 .can any body help me? #include <iostream> using namespace std; #define NUM 42 template <unsigned int N> struct Fibonacci { enum { value = Fibonacci<N - 1>::value + Fibonacci<N -

Assigning unique integral identifier for types, compile-time

瘦欲@ 提交于 2019-12-06 15:20:23
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 it's possible at all, I really don't know what's the hardest part. Edit: How about within a single

Swift - create a fixed length array enforced at compile time

为君一笑 提交于 2019-12-06 13:16:52
问题 I want to enforce (compile time) array with 5 elements of a particular type I couldn't find a solution so resorted to a workaround by creating a tuple (This is abusive I know) typealias FiveElementArray = (MyType,MyType,MyType,MyType,MyType) // mock array by using typed tuple It works for my needs - until I need to access an element by index at runtime. For instance: var DB = FiveElementArray // the tuple of 5 elements tableView(tableView : UITableView,cellForRowAtIndexPath:indexPath) ->

How functions are resolved by compiler?

此生再无相见时 提交于 2019-12-06 11:56:27
问题 How it is determined whether the below call is bound at compile time or at runtime? object.member_fn;//object is either base class or derived class object p->member_fn;//p is either base class or derived class pointer EDITED: #include <iostream> using namespace std; class Base { public: Base(){ cout<<"Constructor: Base"<<endl;} ~Base(){ cout<<"Destructor : Base"<<endl;} }; class Derived: public Base { //Doing a lot of jobs by extending the functionality public: Derived(){ cout<<"Constructor:

method compile time assertion; still not working

不问归期 提交于 2019-12-06 09:35:45
问题 I need a easy way to assert inside a template that a template parameter implements a method ( or one of its parent classes ). I've read Concept check library but is hard to find an easy example to do simple checks like this one. I've tried to follow other posts (like this one and this other one), which i've modified so i can make it generic for many method types (in my example Foo (methodName) and has_foo (Checker name) will, once working correctly, be wrapped as macro arguments so it can be

Compile time operators in C

不问归期 提交于 2019-12-06 07:04:36
问题 I'm familiar with only one compile time operator in C - sizeof . Are there any others that I as a programmer should be aware of? 回答1: Only sizeof that I'm aware, although in C99 sizeof cannot be done at compile time for variable length arrays (VLAs). 回答2: I think what you're grasping for but missing the terminology to describe is a constant expression or integer constant expression . The results of certain operators can be (integer) constant expressions if their operands are, and as you've

Java get compile-time safe method name

妖精的绣舞 提交于 2019-12-06 06:09:51
问题 While working with the reflection class and annotations I have found that there is no clear way to reference a method name in a compile-time safe way. What I really want is to be able to reference a method within an annotation. Might look something like: @CallAfter(method=Foo.class.foo()) void Bar() { ... } At the moment you can only do this with strings, which is not compile time safe.. This is a problem because it undermines Java being statically typed. The only solution I have found is

Creating a non-static version of compiler-based “dictionary” where keys are types

浪子不回头ぞ 提交于 2019-12-06 05:52:29
问题 There is a very easy trick which creates a dictionary-like structure where keys are types. The structure acts like a Dictionary<Type, T?> where keys are Type objects and values are instances of the corresponding types. This wonderful structure is as fast as just a variable or array since the "lookup" is only done once by the compiler/JITter and the proper value reference is compiled into your program. public static class MyDict<T> { public static T Value { get; set; } } You can work with that

How do I parse a string at compile time in Nimrod?

你。 提交于 2019-12-06 03:11:01
问题 Going through the second part of Nimrod's tutorial I've reached the part were macros are explained. The documentation says they run at compile time, so I thought I could do some parsing of strings to create myself a domain specific language. However, there are no examples of how to do this, the debug macro example doesn't display how one deals with a string parameter. I want to convert code like: instantiate(""" height,f,132.4 weight,f,75.0 age,i,25 """) …into something which by hand I would

Array Initialisation Compile Time - Constexpr Sequence

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 21:15:10
问题 I was reading this question on SO. The question itself is not so interesting, but I was wondering whether it exists and how to implement a compile time solution. Regard to the first sequence: All numbers except the ones which can be divided by 3. The sequence should be something like: [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, ...] By induction, I've found the math formula for that sequence: f(0) = 0; f(x > 0) = floor[(3x - 1) / 2]; So I've implemented a C++ constexpr function which generates the i