compile-time

constexpr array and std::initializer_list

北城余情 提交于 2019-11-29 11:27:36
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): _size(values.size()), _data(values.begin()) {} constexpr auto operator[](std::size_t n) -> T { return

C++98/03 std::is_constructible implementation

时光毁灭记忆、已成空白 提交于 2019-11-29 10:20:52
The base components of my hobby library has to work with C++98 and C++11 compilers. To learn and to enjoy myself I created the C++98 implementations of several type support functionality (like enable_if , conditional , is_same , is_integral etc. ...) in order to use them when there is no C++11 support. However while I was implementing is_constructible I got stuck. Is there any kind of template magic (some kind of SFINAE) with which I can implement it without C++11 support ( declval )? Of course there is no variadic template support in C++03, so I will specialise the implementation till some

SASS: Set variable at compile time

微笑、不失礼 提交于 2019-11-29 09:55:24
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 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 Ruby scripts to even query a database. But the code is going to be compiled only once, and served statically

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

随声附和 提交于 2019-11-29 07:47:40
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. (sizeof(array)/sizeof(array[0])) Or as a macro #define ARRAY_SIZE(foo) (sizeof(foo)/sizeof(foo[0])) int array[10]; printf("%d %d\n", sizeof(array), ARRAY_SIZE(array)); 40 10 Caution:

Determining struct member byte-offsets at compile-time?

£可爱£侵袭症+ 提交于 2019-11-29 06:39:01
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! offsetof is a compile time constant, if we look at the draft C++ standard section C.3 C standard library paragraph 2 says: The C++ standard library provides 57 standard macros

How To Get the Name of the Current Procedure/Function in Delphi (As a String)

荒凉一梦 提交于 2019-11-29 01:52:41
问题 Is it possible to obtain the name of the current procedure/function as a string, within a procedure/function? I suppose there would be some "macro" that is expanded at compile-time. My scenario is this: I have a lot of procedures that are given a record and they all need to start by checking the validity of the record, and so they pass the record to a "validator procedure". The validator procedure (the same one for all procedures) raises an exception if the record is invalid, and I want the

Implementing a compile-time “static-if” logic for different string types in a container

戏子无情 提交于 2019-11-28 21:32:27
I'd like to write a function template that operates on a container of strings, for example a std::vector . I'd like to support both CString and std::wstring with the same template function. The problem is that CString and wstring have different interfaces, for example to get the "length" of a CString , you call the GetLength() method, instead for wstring you call size() or length() . If we had a "static if" feature in C++, I could write something like: template <typename ContainerOfStrings> void DoSomething(const ContainerOfStrings& strings) { for (const auto & s : strings) { static_if(strings

What is compile-time polymorphism and why does it only apply to functions?

谁都会走 提交于 2019-11-28 21:21:16
What is compile-time polymorphism and why does it only apply to functions? Way back when, "compile time polymorphism" meant function overloading. It applies only to functions because they're all you can overload. In current C++, templates change that. Neil Butterworth has already given one example. Another uses template specialization. For example: #include <iostream> #include <string> template <class T> struct my_template { T foo; my_template() : foo(T()) {} }; template <> struct my_template<int> { enum { foo = 42 }; }; int main() { my_template<int> x; my_template<long> y; my_template<std:

Compile Time Reflection in C#

萝らか妹 提交于 2019-11-28 18:20:35
I frequently write C# code that has to use magic strings to express property names. Everyone knows the problems with magic strings. They are very difficult to refactor, they have no compile time checking, and often they lead to hard-to-diagnose issues. Yet C#/.NET uses them all over the place to represent property/class/method names. This issue has persisted for years and years, and the only viable solution currently is to use an expression tree which is then parsed at run-time for the property name. This gets you satisfactory compile-time checking, but it complicates the code (requiring

Simplest way to determine return type of function

北城余情 提交于 2019-11-28 18:08:23
Given a very simple, but lengthy function, such as: int foo(int a, int b, int c, int d) { return 1; } // using ReturnTypeOfFoo = ??? What is the most simple and concise way to determine the function's return type ( ReturnTypeOfFoo , in this example: int ) at compile time without repeating the function's parameter types (by name only, since it is known that the function does not have any additional overloads)? NathanOliver You can leverage std::function here which will give you a typedef for the functions return type. This does require C++17 support, since it relies on class template argument