compile-time

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

空扰寡人 提交于 2019-12-05 16:14:42
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? 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 to fill array with contents of a template parameter pack?

≡放荡痞女 提交于 2019-12-05 11:52:42
I had nested partially specialized template code working with VS 2015 until I discovered that it was not standards-compliant . I want it to be so I twisted my code to overcome the former issue and also that one and have now hit a hard wall. Using variadic templates and partial specialization I would like to fill an array at compile-time given a fixed set of parameters. What I want to achieve also seems similar to this answer but I did not manage to make it work. Consider the following program: #include <cstdlib> template <typename T, std::size_t Size> struct Array; template <typename T, std:

How to write Delphi compile-time functions

若如初见. 提交于 2019-12-05 09:56:16
Delphi - can I write my own compile-time functions for const and var declarations, executable at compiler time. Standard Delphi lib contain routines like Ord(), Chr(), Trunc(), Round(), High() etc, used for constant initialization. Can I write my own, to execute routine at compile-time and use the result as constant? Johan You cannot write your own intrinsic functions. Because that requires compiler magic. However there may be other options to achieve your goal. Preprocessor The only way is to use a preprocessor. There are several: http://wiki.delphi-jedi.org/wiki/JEDI_Pre_Processor The Delphi

Can C sort at compile time?

戏子无情 提交于 2019-12-05 06:49:54
Is it possible to sort elements at compile time in C? Syntax is of secondary importance, I was thinking of a macro like this: SORT(9, -1, 12, 4) // expands to: -1, 4, 9, 12 SORT(dog, cat, cow) // expands to: cat, cow, dog but I won't frown at any API as long as it sorts without issuing a single CPU instruction. The requirements are pretty lax: Pure C, no C++ . Staying within the C standard would be nice but established language extensions are fair game. Compiler is the only tool allowed . Unix sort or home-brewed code generators are not welcome as they complicate the build step. Any element

constexpr function not calculate value in compile time

旧街凉风 提交于 2019-12-05 06:39:18
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 - 2>::value }; }; template <> struct Fibonacci<1> { enum { value = 1 }; }; template <> struct Fibonacci

Constexpr variable evaluation

删除回忆录丶 提交于 2019-12-05 02:57:03
问题 Here is my code and I need clarification on what's happening: constexpr int funct(int x){ return x + 1; } int main(){ int x = funct(10); return 0; } constexpr 's allows compile time calculation, and based on my code above, since funct is declared as constexpr , it is allowed to do compile time calculations if the arguments are constants or constexpr's themselves. The part that I am confused with lies in this part, the int x . Since it is not declared as constexpr , would it mean that int x

Compute nth prime at compile time [closed]

匆匆过客 提交于 2019-12-05 01:02:55
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . The C++11 features, with constexpr and template argument packs, should in my opinion be strong enough to perform some rather complex computations. One

Finding endian-ness programmatically at compile-time using C++11

本秂侑毒 提交于 2019-12-04 23:50:29
I have referred many questions in SO on this topic, but couldn't find any solution so far. One natural solution was mentioned here: Determining endianness at compile time . However, the related problems mentioned in the comments & the same answer. With some modifications, I am able to compile a similar solution with g++ & clang++ ( -std=c++11 ) without any warning. static_assert(sizeof(char) == 1, "sizeof(char) != 1"); union U1 { int i; char c[sizeof(int)]; }; union U2 { char c[sizeof(int)]; int i; }; constexpr U1 u1 = {1}; constexpr U2 u2 = {{1}}; constexpr bool IsLittleEndian () { return u1

Swift - create a fixed length array enforced at compile time

空扰寡人 提交于 2019-12-04 19:47:28
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) -> UITableViewCell { // would like to populate with the value at index DB[indexpath.row] // no such syntax for

Confusion about constant expressions

最后都变了- 提交于 2019-12-04 17:31:48
问题 This is some kind of follow-up for this topic and deals about a little part of it. As with the previous topic, let's consider that our compiler has constexpr functions for std::initializer_list and std::array . Now, let's go straight to the point. This works: #include <array> #include <initializer_list> int main() { constexpr std::array<int, 3> a = {{ 1, 2, 3 }}; constexpr int a0 = a[0]; constexpr int a1 = a[1]; constexpr int a2 = a[2]; constexpr std::initializer_list<int> b = { a0, a1, a2 };