compile-time

[C++ compile time assertions]: Can we throw a compilation error if some condition is not met?

半腔热情 提交于 2020-01-24 03:23:04
问题 I wrote a function: template<int N> void tryHarder() { for(int i = 0; i < N; i++) { tryOnce(); } } but I only want it to compile if N is in between 0 and 10. Can I do it? How? 回答1: You can do it with static_assert declaration: template<int N> void tryHarder() { static_assert(N >= 0 && N <= 10, "N out of bounds!"); for(int i = 0; i < N; i++) { tryOnce(); } } This feature is only avaliable since C++11. If you're stuck with C++03, take a look at Boost's static assert macro. The whole idea of

Obtaining the Build ID in a Java Application

半腔热情 提交于 2020-01-23 08:31:36
问题 Does anyone have a simple suggestion for recording a build ID (generated at compile time) which is displayed in the title bar of the app at runtime? Building from within Eclipse, all I need is the ID, I can then pass it up to the title. 回答1: If you are using Ant, you can easily set up your "jar" or "package" target so that it generates a file including the current timestamp and include this in your jar output. If using Maven, there are a few ways to achieve something similar, such as dropping

Obtaining the Build ID in a Java Application

落花浮王杯 提交于 2020-01-23 08:30:12
问题 Does anyone have a simple suggestion for recording a build ID (generated at compile time) which is displayed in the title bar of the app at runtime? Building from within Eclipse, all I need is the ID, I can then pass it up to the title. 回答1: If you are using Ant, you can easily set up your "jar" or "package" target so that it generates a file including the current timestamp and include this in your jar output. If using Maven, there are a few ways to achieve something similar, such as dropping

In C#, how to restrict who can call a method at compile time

Deadly 提交于 2020-01-11 06:43:07
问题 In C#, is it possible to restrict who can call a method at compile time? I've looked into directives, but that didn't work since I can't assign values to symbols. #define WHO VisualStudioUser.Current // does not work I also looked into Code Access Security (CAS) but that's runtime enforcement, not compile time. The requirement is to restrict access to a method at compile time for specific developers given the method exists in a pre-compiled assembly. here's more details... I'm building a

C++98/03 std::is_constructible implementation

旧巷老猫 提交于 2020-01-10 03:53:05
问题 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

C++ Type Traits Overview [closed]

会有一股神秘感。 提交于 2020-01-06 12:42:16
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . Has anybody put together a list of all the type traits available in standard <type_traits> (GCC-4.6.1) and Boost's own <boost/type_traits.hpp> ? 回答1: The full lists of traits are available online: The Boost

3 different / same ways of doing N-factorial compile time in C++

淺唱寂寞╮ 提交于 2020-01-06 04:45:08
问题 I am trying to play with template metaprogramming, constexpr and if constexpr and have come up with 3 different ways of doing a N-recursive / N-factorial operation. All three examples are some I've found here on SO or by searching on the net - and then modified it, so they do the same The first example is using template metaprogramming: example 1 template<int N> struct NGenerator { static const int result = N + NGenerator<N-1>::result; }; template<> struct NGenerator<0> { static const int

Checking for the existence a reference/type at compile time in .NET

Deadly 提交于 2020-01-04 14:04:33
问题 I've recently found the need to check at compile-time whether either: a) a certain assembly reference exists and can be successfully resolved, or b) a certain class (whose fully qualified name is known) is defined. These two situations are equivalent for my purposes, so being able to check for one of them would be good enough. Is there any way to do this in .NET/C#? Preprocessor directives initially struck me as something that might help, but it seems it doesn't have the necessary capability.

Can C sort at compile time?

核能气质少年 提交于 2020-01-02 03:25:13
问题 Is it possible to sort elements at compile time in C? Syntax is of secondary importance, I was thinking of a macro like this: SORT(9, -1, 12, 4) // expands to: -1, 4, 9, 12 SORT(dog, cat, cow) // expands to: cat, cow, dog but I won't frown at any API as long as it sorts without issuing a single CPU instruction. The requirements are pretty lax: Pure C, no C++ . Staying within the C standard would be nice but established language extensions are fair game. Compiler is the only tool allowed .

Can C sort at compile time?

我只是一个虾纸丫 提交于 2020-01-02 03:24:31
问题 Is it possible to sort elements at compile time in C? Syntax is of secondary importance, I was thinking of a macro like this: SORT(9, -1, 12, 4) // expands to: -1, 4, 9, 12 SORT(dog, cat, cow) // expands to: cat, cow, dog but I won't frown at any API as long as it sorts without issuing a single CPU instruction. The requirements are pretty lax: Pure C, no C++ . Staying within the C standard would be nice but established language extensions are fair game. Compiler is the only tool allowed .