compile-time

How can I print the result of sizeof() at compile time in C?

南楼画角 提交于 2019-11-26 15:23:49
问题 How can I print the result of sizeof() at compile time in C? For now I am using a static assert (home brewed based on other web resources) to compare the sizeof() result to various constants. While this works... it is far from elegant or fast. I can also create an instance of the variable/struct and look in the map file but this is also less elegant and fast than a direct call/command/operator. Further, this is an embedded project using multiple cross-compilers... so building and loading a

General rules of passing/returning reference of array (not pointer) to/from a function?

我们两清 提交于 2019-11-26 15:14:38
问题 We can pass reference of an array to a function like: void f(int (&a)[5]); int x[5]; f(x); //okay int y[6]; f(y); //error - type of y is not `int (&)[5]`. Or even better, we can write a function template: template<size_t N> void f(int (&a)[N]); //N is size of the array! int x[5]; f(x); //okay - N becomes 5 int y[6]; f(y); //okay - N becomes 6 Now my question is, how to return reference of an array from a function? I want to return array of folllowing types from a function: int a[N]; int a[M]

Calculating and printing factorial at compile time in C++

杀马特。学长 韩版系。学妹 提交于 2019-11-26 14:06:23
问题 template<unsigned int n> struct Factorial { enum { value = n * Factorial<n-1>::value}; }; template<> struct Factorial<0> { enum {value = 1}; }; int main() { std::cout << Factorial<5>::value; std::cout << Factorial<10>::value; } above program computes factorial value during compile time. I want to print factorial value at compile time rather than at runtime using cout. How can we achive printing the factorial value at compile time? I am using VS2009. Thanks! 回答1: The factorial can be printed

Can a program depend on a library during compilation but not runtime?

依然范特西╮ 提交于 2019-11-26 12:51:09
问题 I understand the difference between runtime and compile-time and how to differentiate between the two, but I just don\'t see the need to make a distinction between compile-time and runtime dependencies . What I\'m choking on is this: how can a program not depend on something at runtime that it depended on during compilation? If my Java app uses log4j, then it needs the log4j.jar file in order to compile (my code integrating with and invoking member methods from inside log4j) as well as

C++ Get name of type in template

夙愿已清 提交于 2019-11-26 12:37:06
问题 I\'m writing some template classes for parseing some text data files, and as such it is likly the great majority of parse errors will be due to errors in the data file, which are for the most part not written by programmers, and so need a nice message about why the app failed to load e.g. something like: Error parsing example.txt. Value (\"notaninteger\")of [MySectiom]Key is not a valid int I can work out the file, section and key names from the arguments passed to the template function and

Is is_constexpr possible in C++11?

偶尔善良 提交于 2019-11-26 11:49:43
Is it possible to produce a compile-time boolean value based on whether or not a C++11 expression is a constant expression (i.e. constexpr ) in C++11? A few questions on SO relate to this, but I don't see a straight answer anywhere. As of 2017, is_constexpr is not possible in C++11. That sounds like an odd thing to say, so let me explain a bit of the history. First, we added this feature to resolve a defect: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1129 Johannes Schaub - litb posted a constexpr detection macro that relied on the provision that constant expressions are

Compile time vs Run time Dependency - Java

痴心易碎 提交于 2019-11-26 10:09:48
问题 What is the difference between compile time and run time dependencies in Java? It is related to class path, but how do they differ? 回答1: Compile-time dependency : You need the dependency in your CLASSPATH to compile your artifact. They are produced because you have some kind of "reference" to the dependency hardcoded in your code, such as calling new for some class, extending or implementing something (either directly or indirectly), or a method call using the direct reference.method()

Check at Compile-Time if Template Argument is void

倖福魔咒の 提交于 2019-11-26 05:38:35
问题 I\'m trying to wrap the Windows API functions to check errors when I so choose. As I found out in a previous SO question, I could use a template function to call the API function, and then call GetLastError() to retrieve any error it might have set. I could then pass this error to my Error class to let me know about it. Here\'s the code for the template function: template<typename TRet, typename... TArgs> TRet Wrap(TRet(WINAPI *api)(TArgs...), TArgs... args) { TRet ret = api(args...); //check

Detecting CPU architecture compile-time

核能气质少年 提交于 2019-11-26 04:38:52
问题 What is the most reliable way to find out CPU architecture when compiling C or C++ code? As far as I can tell, different compilers have their own set of non-standard preprocessor definitions ( _M_X86 in MSVS, __i386__ , __arm__ in GCC, etc). Is there a standard way to detect the architecture I\'m building for? If not, is there a source for a comprehensive list of such definitions for various compilers, such as a header with all the boilerplate #ifdef s? 回答1: Here is some information about Pre

Is sizeof in C++ evaluated at compilation time or run time?

两盒软妹~` 提交于 2019-11-26 04:21:25
问题 For example result of this code snippet depends on which machine: the compiler machine or the machine executable file works? sizeof(short int) 回答1: sizeof is a compile time operator. 回答2: It depends on the machine executing your program. But the value evaluates at compile time. Thus the compiler (of course) has to know for which machine it's compiling. 回答3: sizeof is evaluated at compile time, but if the executable is moved to a machine where the compile time and runtime values would be