compile-time

Can I obtain C++ type names in a constexpr way?

我怕爱的太早我们不能终老 提交于 2019-11-27 20:28:05
I would like to use the name of a type at compile time. For example, suppose I've written: constexpr size_t my_strlen(const char* s) { const char* cp = s; while(*cp != '\0') { cp++; }; return cp - s; } and now I want to have: template <typename T> constexpr auto type_name_length = my_strlen(typeid(T).name()); But alas, typeid(T).name() is just const char* , not constexpr... is there some other, constexpr way to get a type's name? Well, you could, sort of, but probably not quite portable: struct string_view { char const* data; std::size_t size; }; inline std::ostream& operator<<(std::ostream& o

Extracting Property Names For Reflection, with Intellisense and Compile-Time Checking

别说谁变了你拦得住时间么 提交于 2019-11-27 13:53:55
问题 Ok. So I have some code that maps certain controls on a winForm to certain properties in an object, in order to do certain things to the controls when certain things happen to the data. All well and good, works fine. Not the problem. The issue is, to add items to the mapping, I call a function that looks like: this.AddMapping(this.myControl,myObject,"myObjectPropertyName"); The problem I run into is that it is very difficult to tell, at compile time, the difference between the above line and

Can I make a constant from a compile-time env variable in csharp?

蹲街弑〆低调 提交于 2019-11-27 13:01:56
问题 We use Hudson to build our projects, and Hudson conveniently defines environment variables like "%BUILD_NUMBER%" at compile time. I'd like to use that variable in code, so we can do things like log what build this is at run time. However I CAN NOT do System.Environment.GetEnvironmentVariable because that is accessing the run-time environment, what I want is something like: #define BUILD_NUM = %BUILD_NUMBER% or const string BUILD_NUM = %BUILD_NUMBER% Except I don't know the syntax. Can someone

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

旧城冷巷雨未停 提交于 2019-11-27 11:19:50
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 sample program to the target and then reading out a value is even more of a hassle than either of the

Simplest way to determine return type of function

时间秒杀一切 提交于 2019-11-27 11:04:28
问题 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)? 回答1: You can leverage std::function here which will give you an alias for the

Compile Time Reflection in C#

泪湿孤枕 提交于 2019-11-27 10:54:02
问题 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

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

百般思念 提交于 2019-11-27 10:34:25
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][N]; int (*a)[N]; int (*a)[M][N]; where M and N is known at compile time! What are general rules for

Detecting Endianness

元气小坏坏 提交于 2019-11-27 08:39:34
I'm currently trying to create a C source code which properly handles I/O whatever the endianness of the target system. I've selected "little endian" as my I/O convention, which means that, for big endian CPU, I need to convert data while writing or reading. Conversion is not the issue. The problem I face is to detect endianness, preferably at compile time (since CPU do not change endianness in the middle of execution...). Up to now, I've been using this : #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ ... #else ... #endif It's documented as a GCC pre-defined macro, and Visual seems to

Calculating and printing factorial at compile time in C++

点点圈 提交于 2019-11-27 08:17:13
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! The factorial can be printed in compiler-generated message as: template<int x> struct _; int main() { _<Factorial<10>::value> __; return 0

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

核能气质少年 提交于 2019-11-27 06:03:32
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 runtime (my code has absolutely no control over what happens once code inside log4j.jar is ran). I'm reading up