compile-time

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

醉酒当歌 提交于 2019-12-17 15:53:47
问题 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? 回答1: Well, you could, sort of, but probably not quite portable:

Prevent header from being included in some files, in compilation time?

醉酒当歌 提交于 2019-12-14 04:17:55
问题 I have a header file, which I can control its contents. Additionally I have an interface I1 (defined in some other file) from which various implementations derived. I want to prohibit those implementations from including this header file. So that during compile time if the file is included the compilation will fail, otherwise it will continue as usual. So I have header file and an interface definition (in some other file). I want to prohibit interface implementations from including the given

if vs if constexpr inside constexpr function

可紊 提交于 2019-12-13 14:21:12
问题 Recently I modify some if constexpr into if in my constexpr functions and found they still work fine and can be evaluated when compile time. Here is a minimum case: template<int N> constexpr bool is_negative() { if constexpr (N >= 0) return false; else return true; } int main() { constexpr bool v = is_negative<1>(); } live demo In the case above, N must be known at compile time because it is non-type template parameter, so if constexpr works fine here. However, it is a constexpr function, so,

Is inlining done at compile time or run-time?

戏子无情 提交于 2019-12-13 10:22:38
问题 I used to think that the compiler decides whether to inline a function or not at compile time. But then I found this code example in "Effective C++": inline void f() {} // assume compilers are willing to inline calls to f void (*pf)() = f; // pf points to f f(); // this call will be inlined, because it's a "normal" call pf(); // this call probably won't be, because it's through a function pointer Now I'm confused, does that mean the decision whether to inline a function or not is done at run

Effective way of detecting X11 vs Wayland, preferrably with CMake

与世无争的帅哥 提交于 2019-12-12 19:39:32
问题 So I've done some Google searching and this is something that has very little knowledge out there. What would be an effective and foolproof way of detecting whether X11 or Wayland is in use, preferrably at compile-time and with CMake? I need to apply this to a C++ project of mine. 回答1: I assume you want to evaluate the display server during compile time, when calling CMake, instead of for every compilation. That's how CMake works and hot it should be used. One downside is, that you have to re

Get the type information in macros

旧城冷巷雨未停 提交于 2019-12-12 18:21:54
问题 (defmacro test (&key list &environment env) (typecase (get-type list env) (list `(do-something (list ,@list))) (integer `(do-something (list ,list ,list ,list))))) (test :list '(1 2 3)) ; => (do-something (list 1 2 3)) (test :list (* 1 2)) ; => (do-something (list (* 1 2) (* 1 2) (* 1 2))) Is it possible to get the type of list at macroexpansion-time? (what I called get-type ) It won't work with evaluation, because the variables may not be existent at this time, and maybe some other values

How to use different overload of an inline function, depending on a compile time parameter?

让人想犯罪 __ 提交于 2019-12-12 16:15:43
问题 I have a performance critical inline function, inline T func(T a, T b, int p) . It can be optimized quite a bit, if p is known to be zero. However, I can't use an 'if' and penalize all the the other cases. What I want is to optimize the function only of I know at compile-time that p is zero. Is there a clean way to do that, maybe using template magic? EDIT I can't use differently named function/incompatible overloads (I don't know ho to express that correctly) since the code is very low level

convincing C# compiler that execution will stop after a member returns

旧街凉风 提交于 2019-12-12 12:25:09
问题 I don't think this is currently possible or if it's even a good idea, but it's something I was thinking about just now. I use MSTest for unit testing my C# project. In one of my tests, I do the following: MyClass instance; try { instance = getValue(); } catch (MyException ex) { Assert.Fail("Caught MyException"); } instance.doStuff(); // Use of unassigned local variable 'instance' To make this code compile, I have to assign a value to instance either at its declaration or in the catch block. I

Is it possible to have a compile time check that a type is marked with the Serializable attribute

跟風遠走 提交于 2019-12-12 07:24:50
问题 Specifically we're making our application compatible with the Out Of Process Session State server where all types saved in session must be serializable. Is there a way to see at compile time that any type put into HttpSessionState is marked with the Serializable attribute. Something along the lines of this 'non-valid' code public static void Put<T>( string key, T value ) where T : IsMarkedWitheSerializableAttribute { HttpContext.Current.Session[key] = value; } 回答1: You could write a custom

Scala TypeTag Reflection returning type T

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 14:26:17
问题 I currently have this: def stringToOtherType[T: TypeTag](str: String): T = { if (typeOf[T] =:= typeOf[String]) str.asInstanceOf[T] else if (typeOf[T] =:= typeOf[Int]) str.toInt.asInstanceOf[T] else throw new IllegalStateException() I would REALLY like to not have the .asInstanceOf[T] if possible (runtime). Is this possible? Removing the asInstanceOf gives me a type of Any, which makes sense, but since we are using reflection and know for sure that I am returning a value of type T, I don't see