compile-time

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

て烟熏妆下的殇ゞ 提交于 2019-11-28 11:11:47
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 | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) elementToTree :: a -> Tree a elementToTree x =

Does “undefined behaviour” extend to compile-time?

断了今生、忘了曾经 提交于 2019-11-28 09:08:51
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? Filip Roséen - refp " You're all ignoring the actual definition and focusing on the note, The standard imposes no requirements . " - @ R.MartinhoFernandes The message above was

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

有些话、适合烂在心里 提交于 2019-11-28 07:09:51
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-value)? BTW: I know I can insert into the map using Map.insert(map<K,V>::value_type(k,v)) . Edit: several

Compile-time or runtime detection within a constexpr function

主宰稳场 提交于 2019-11-28 05:27:05
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 also be callable at runtime, I figured that this would be a way to ensure that MyMin can only ever be

constexpr array and std::initializer_list

青春壹個敷衍的年華 提交于 2019-11-28 04:49:30
问题 I was trying to write an compile-time valarray that could be used like this: constexpr array<double> a = { 1.0, 2.1, 3.2, 4.3, 5.4, 6.5 }; static_assert(a[0] == 1.0, ""); static_assert(a[3] == 4.3, ""); static_assert(a.size() == 6, ""); I managed to do it with the following implementation and it works fine (with GCC 4.7): #include <initializer_list> template<typename T> struct array { private: const std::size_t _size; const T* _data; public: constexpr array(std::initializer_list<T> values):

SASS: Set variable at compile time

孤街浪徒 提交于 2019-11-28 03:12:09
问题 Is it possible to set a sass variable at compile time? I basically want to do this: $color: red !default; div#head { background-color: $color; } When I compile to css I want to set $color to "blue" (preferably from the command line). Has anyone been able to do this? Thanks, Chris 回答1: I found this at their FAQ http://sass-lang.com/docs/yardoc/file.FAQ.html If you just want to pass some variables to the CSS every time it gets compiled, like using --watch , you can use Sass functions to define

How to determine the length of an array at compile time?

倖福魔咒の 提交于 2019-11-28 01:24:42
问题 Are there macros or builtins that can return the length of arrays at compile time in GCC? For example: int array[10]; For which: sizeof(array) == 40 ???(array) == 10 Update0 I might just point out that doing this in C++ is trivial. One can build a template that returns the number inside [] . I was certain that I'd once found a lengthof and dimof macro/builtin in the Visual C++ compiler but cannot find it anymore. 回答1: (sizeof(array)/sizeof(array[0])) Or as a macro #define ARRAY_SIZE(foo)

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

折月煮酒 提交于 2019-11-28 00:52:00
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? Some compilers will evaluate them at compile time, but this behavior is not guaranteed (and doing so may also cause problems). You'll need to check your compiler and see what it does. Note that on many systems, log(2) is available from the

Determining struct member byte-offsets at compile-time?

北城余情 提交于 2019-11-28 00:05:20
问题 I want to find the byte offset of a struct member at compile-time. For example: struct vertex_t { vec3_t position; vec3_t normal; vec2_t texcoord; } I would want to know that the byte offset to normal is (in this case it should be 12 .) I know that I could use offsetof , but that is a run-time function and I'd prefer not to use it. Is what I'm trying to accomplish even possible? EDIT : offsetof is compile-time, my bad! 回答1: offsetof is a compile time constant, if we look at the draft C++

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

我只是一个虾纸丫 提交于 2019-11-27 22:23:18
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 )? base * POW(base, expo -1) : 1; } template<typename T> void foobar(T val) { std::cout << val << std::endl;