compile-time

Why in C a const object is not a compile-time constant expression? [duplicate]

拟墨画扇 提交于 2019-12-11 08:02:24
问题 This question already has answers here : Can a const variable be used to declare the size of an array in C? (5 answers) Closed 3 years ago . In C the const qualifier makes an object read-only but not a constant expression. For example, it is not possible to use a const int variable to dimension an array: const int n = 10; int arr [n]; /* Compile-time error */ Which is the technical reason for this? Is it not possible for the compiler at compile-time to know that the object has actually a

Do I Have to Specialize Templates If Their Offending Code Is in an if(false)

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 05:54:38
问题 Given the hierarchy: struct base {}; struct a : public base {}; struct b : public base {}; I want to fill vector<base*> vecBase and vector<a*> aVec with this function: template <typename T> void foo(T* bar) { if (is_base_of_v<decltype(baseVec)::value_type, T>) baseVec.push_back(static_cast<decltype(baseVec)::value_type>(bar)); if (is_base_of_v<decltype(aVec)::value_type, T>) baseVec.push_back(static_cast<decltype(aVec)::value_type>(bar)); } The problem here is that even though the static_cast

compile time type at runtime

眉间皱痕 提交于 2019-12-11 02:47:10
问题 Is there any way in Java to get the compile time type of a reference at runtime? Example: private void doSomething(final Object o) { // do somthing } final Number n = 1; doSomething(n); final Object o = 1; doSomething(o); final Integer i = 1; doSomething(i); 1st call --> Number 2nd call --> Object 3rd call --> Integer Edit: This is a very simplified version of the problem. What i am trying to do is to detect(instead of being told) inside a framework metadata about objects being passed. What

Floating point arithmetic at compile-time

我只是一个虾纸丫 提交于 2019-12-11 01:13:50
问题 Are floating point calculations, which use compile-time constant integers, performed during compile-time or during run-time? For example, when is the division operation calculated in: template <int A, int B> inline float fraction() { return static_cast<float>(A) / B; } 回答1: I believe it is implementation defined, but most compilers will evaluate constant expressions at compile time. However even if yours does not the following modification: template <int A, int B> inline float fraction() {

Forbids functions with `static_assert`

做~自己de王妃 提交于 2019-12-11 01:06:28
问题 I want to prevent certain functions from being called. Let's ignore the case of calling the function via a function pointer or something, and just concentrate on the case of direct function call. I can do this with = delete . However, the diagnostic issued is not quite informative. I considered using static_assert , with which you can supply a custom diagnostic message. I placed a static_assert(false, ...) statement within the function body, hoping that it fires when the function is called.

How to protect enum assignment

社会主义新天地 提交于 2019-12-10 19:20:59
问题 I want to prevent invalid value enum assignment. I know if i even assign value that is not in enum it will work. Example: enum example_enum { ENUM_VAL0, ENUM_VAL1, ENUM_VAL2, ENUM_VAL3 }; void example_function(void) { enum example_enum the_enum = ENUM_VAL3; // correct the_enum = 41; // will work the_enum = 43; // also will work bar(the_enum); // this function assumes that input parameter is correct } Is there easy, efficient way to check if assignment to enum is correct? I could test value by

C++11 - Compile time Polymorphism solutions

泪湿孤枕 提交于 2019-12-10 17:24:06
问题 Suppose that I'm writing a cross-platform library, I have to organize the code in a way that there is a different behaviour for different platforms and this behaviour ( or definition ) it's choosen at compile time based on the platform where my library it's being compiled. The "usual" way to do this in C++ is to pollute the code with a lot of #ifdef when writing a method or a class. The problem with approach is that: the source code looks really ugly if you are supporting 3 platforms, your

Run Nim code at compile time

点点圈 提交于 2019-12-10 17:08:46
问题 So I know that if I define a const Nim will evaluate whatever I assign to it at compile time, so I could do something like this: proc compileTimeCode: bool = # Put code here return true const _ = compileTimeCode() and then I could put my code in the compileTimeCode proc. This works, but seems messy, overcomplicated and unintuitive. It also requires more typing than it should, and is difficult to DRY up. 回答1: What's the question? If there is a better way to run code at compile time? static: #

Compile time Meta-programming, with string literals

爱⌒轻易说出口 提交于 2019-12-10 12:17:33
问题 I'm writing some code which could really do with some simple compile time metaprogramming. It is common practise to use empty-struct tags as compile time symbols. I need to decorate the tags with some run-time config elements. static variables seem the only way to go (to enable meta-programming), however static variables require global declarations. to side step this Scott Myers suggestion (from the third edition of Effective C++), about sequencing the initialization of static variables by

How to write Delphi compile-time functions

江枫思渺然 提交于 2019-12-10 05:30:45
问题 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? 回答1: 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