template-meta-programming

Removing mutations for D metaprogramming/compiletime array generation

邮差的信 提交于 2019-12-14 02:38:46
问题 My plan is to write a mutation-less code in D-language so that my values are available by runtime. Someone spoke to me about loop-unrolling and compile time code generation but I have no clear idea how to do that. I have made the D-template below but it has no guarantee to be evaluated at compile-time because on the two assignment statements(mutations) . Advice would be greatly appreciated. Suggestions could be preferably in D or C++ without macros. import std.stdio; import std.string; import

How can I (at compile time) determine if a typename is a function pointer typename?

丶灬走出姿态 提交于 2019-12-13 14:18:43
问题 Consider the following wrapper around Win32's Runtime Dynamic Linking mechanism: #include <boost/noncopyable.hpp> #include <windows.h> #include "Exception.hpp" namespace WindowsApi { class RuntimeDynamicLinker : boost::noncopyable { HMODULE hMod_; public: RuntimeDynamicLinker(const wchar_t * moduleName) { hMod_ = LoadLibraryW(moduleName); if (hMod_ == 0) { Exception::Throw(GetLastError()); } } template <typename T> T GetFunction(const char* functionName) { FARPROC result = GetProcAddress(hMod

How to self register class instances using the CRTP?

我的梦境 提交于 2019-12-13 11:31:30
问题 I have a registry for lambda functions associated with a specific CommandId , where the registered function is supposed to create a concrete instance of a command executor class and supply it with some data: CommandId.h #include <cstdint> enum class CommandId : uint16_t { Command1 , Command2 , }; Registry.h #include <map> #include <functional> #include <string> #include "CommandId.h" class Registry { public: static std::map<CommandId,std::function<void (std::string)>>& GetCommands() { static

Turning a BOUNDED std::list<class> of parameters into a type std::tuple<class,class,class> tup<classObj1, classObj2,classObj2>

老子叫甜甜 提交于 2019-12-13 10:11:05
问题 I have a class class C { public: C() {} private: int timesTriggered_; std::map<std::string, std::tuple<std::string, int, int, int, int, int>> mapST; std::vector<std::string> sv; }; and some objects of this type: C c1; C c2; C c3; I have a map indexed by strings of class C std::map<std::string, std::list<C>> cMap; I add some objects of class C into a map cMap["1"].push_back(c1); cMap["2"].push_back(c2); cMap["3"].push_back(c3); I need a function that when passed the std::list will return (for

Dump variadic template content to a 2D array

时光毁灭记忆、已成空白 提交于 2019-12-13 04:54:26
问题 Brief description: Consider a variadic-template based typelist used to hold integral values: template<typename... Ts> struct list {}; using my_int_list = list<std::integral_constant<0>, std::integral_constant<1>, std::integral_constant<2>>; This could be dumped to an array using array initializers and variadic pack expansion: template<typename LIST> struct to_array; template<typename... Ts> struct to_array<list<Ts...>> { static constexpr unsigned int result[] = { Ts::value... }; }; Now

How to implement a “BaseState” with access to back/front end of the state-machine (SM) in boost::msm

…衆ロ難τιáo~ 提交于 2019-12-13 03:57:14
问题 I want to share data and access between states as well as the SM as a whole and the client code (i.e. the code outside the SM). Based on what I've come up with on the net, the best way would be to inherit all states from a base class. Adding a base class and making all states & the SM to inherit from that is simple, but how can I add the handler to the backend/frontend of the SM as a member of this base class and how can I initialize it? This sample code compiles, but crashes when accessing

Writing constructor of template class with template argument-depending parameters

£可爱£侵袭症+ 提交于 2019-12-13 03:44:14
问题 Consider the following two classes with constructors that accept wildly different sets of parameters. class A { public: A(int x, double y) { //do something } }; class B { public: B(const std::vector<bool>& x) { //do something } }; Now, I want to write a class template TpC that will have an object (which might be A or B or something else). In other words, I want to be able to use the following: int x; double y; std::vector<bool> z; TpC<A> obj_a (x, y); TpC<B> obj_b (z); How can I write a

Flattening a pack of types, where non-type values are part of the flattening

╄→尐↘猪︶ㄣ 提交于 2019-12-13 02:59:47
问题 If you look at the member type of template <typename Pack> struct flatten; template <typename T, typename U, std::size_t A, std::size_t B, std::size_t C, typename V, std::size_t D, std::size_t E, typename W> struct flatten<std::tuple<T,U, std::index_sequence<A,B,C>, V, std::index_sequence<D,E>, W>> { template <typename, typename, std::size_t, std::size_t, std::size_t, typename, std::size_t, std::size_t, typename> struct S; using type = S<T,U,A,B,C,V,D,E,W>; }; is it possible to do this

Computing the type of a function pointer

牧云@^-^@ 提交于 2019-12-13 02:44:59
问题 Consider the following: template<typename T> struct S { typedef M< &T::foo > MT; } This would work for: S<Widget> SW; where Widget::foo() is some function How would I modify the definition of struct S to allow the following instead: S<Widget*> SWP; 回答1: What you need is the following type transformation. given T , return T given T * , return T It so happens that the standard library already has implemented this for us in std::remove_pointer (though it's not hard to do yourself). With this,

How can the “min” type be found in a variadic parameter pack?

爱⌒轻易说出口 提交于 2019-12-12 19:31:23
问题 By "min" type, I mean the type compared less than all according to a compile time function, eg sizeof I have a draft implementation, will present it first and refer to the two problems I'm facing: #include <iostream> #include <typeinfo> #include <type_traits> using namespace std; // Unspecialized version template<typename...Ts> struct Tmin { using type = void; }; template<typename T> struct Tmin<T> { using type = T; }; template<typename T1, typename T2, typename...Ts> struct Tmin<T1, T2, Ts..