compile-time

How to get compile time type of a variable?

空扰寡人 提交于 2019-11-27 06:02:24
问题 I'm looking for how to get compile time type of a variable for debugging purposes. The testing environment can be reproduced as simply as: object x = "this is actually a string"; Console.WriteLine(x.GetType()); Which will output System.String . How could I get the compile time type System.Object here? I took a look over at System.Reflection , but got lost in the amount of possibilities it provides. 回答1: I don't know if there is a built in way to do it but the following generic method would do

Why are (constant) expressions not evaluated at compile time in Haskell?

跟風遠走 提交于 2019-11-27 05:59:37
问题 I am currently learning Haskell, and there is one thing that baffles me: When I build a complex expression (whose computation will take some time) and this expression is constant (meaning it is build only of known, hard coded values), the expression is not evaluated at compile time. Comming from a C/C++ background I am used to such kind of optimization. What is the reason to NOT perform such optimization (by default) in Haskell / GHC ? What are the advantages, if any? data Tree a = EmptyTree

Do math functions of constant expressions get pre-calculated at compile time?

匆匆过客 提交于 2019-11-27 04:45:47
问题 I tend to use math functions of constant expressions for convinience and coherence (i.e log(x)/log(2) instead of log(x)/0.3... ). Since these functions aren't actually a part of the language itself, neither are they defined in math.h (only declared), will the constant ones get precalculated at compile time, or will they be wastefully calculated at runtime? 回答1: Some compilers will evaluate them at compile time, but this behavior is not guaranteed (and doing so may also cause problems). You'll

Forcing a constant expression to be evaluated during compile-time?

跟風遠走 提交于 2019-11-27 04:34:19
问题 A few days ago I asked by which criteria the compiler decides whether or not, to compute a constexpr function during compile time. When does a constexpr function get evaluated at compile time? As it turns out, a constexpr is only evaluated during compile-time, if all parameters are constant expressions and the variable you are assigning it to is are constant expression as well. template<typename base_t, typename expo_t> constexpr base_t POW(base_t base, expo_t expo) { return (expo != 0 )?

C++ Get name of type in template

半世苍凉 提交于 2019-11-27 02:52:05
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 member vars in the class, however I'm not sure how to get the name of the type the template function is

Using std::map<K,V> where V has no usable default constructor

十年热恋 提交于 2019-11-27 01:44:55
问题 I have a symbol table implemented as a std::map . For the value, there is no way to legitimately construct an instance of the value type via a default constructor. However if I don't provide a default constructor, I get a compiler error and if I make the constructor assert, my program compile just fine but crashes inside of map<K,V>::operator [] if I try to use it to add a new member. Is there a way I can get C++ to disallow map[k] as an l-value at compile time (while allowing it as an r

Compile-time or runtime detection within a constexpr function

余生长醉 提交于 2019-11-27 00:56:08
问题 I was excited when constexpr was introduced in C++11, but I unfortunately made optimistic assumptions about its usefulness. I assumed that we could use constexpr anywhere to catch literal compile-time constants or any constexpr result of a literal compile-time constant, including something like this: constexpr float MyMin(constexpr float a, constexpr float b) { return a<b?a:b; } Because qualifying a function's return type only as constexpr does not limit its usage to compile-time, and must

Does “undefined behaviour” extend to compile-time?

我怕爱的太早我们不能终老 提交于 2019-11-26 18:24:11
问题 We've all heard the warnings that if you invoke undefined behaviour in C or C++, anything at all can happen. Is this limited to any runtime behaviour at all , or does this also include any compile-time behaviour? In particular, is a compiler, upon encountering a construct that invokes undefined behaviour, allowed to reject the code (in the absence of other requirements in the standard to do so), or even to crash? 回答1: " You're all ignoring the actual definition and focusing on the note, The

Check at Compile-Time if Template Argument is void

大兔子大兔子 提交于 2019-11-26 17:55:45
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 for errors return ret; } Using this I can have code as follows int WINAPI someFunc (int param1, BOOL

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

廉价感情. 提交于 2019-11-26 16:07:45
For example result of this code snippet depends on which machine: the compiler machine or the machine executable file works? sizeof(short int) sizeof is a compile time operator. 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. sizeof is evaluated at compile time, but if the executable is moved to a machine where the compile time and runtime values would be different, the executable will not be valid. As of C99, sizeof is evaluated at runtime if and only if the operand is a