template-meta-programming

SFINAE to make base template always result in error

心不动则不痛 提交于 2019-12-29 07:52:49
问题 So I'm designing a sort of my_numeric_cast function to limit the types of conversions available when using a framework I'm writing. It was pretty straight forward to do something like template<typename To, typename From> constexpr To my_numeric_cast(From); template<> constexpr float my_numeric_cast<float, int>(int i) { return i; } Which works, allowing only casting from ints to floats whenever the cast is used. And producing a linkage error whenever a cast not in the white list is attempted.

SFINAE to make base template always result in error

前提是你 提交于 2019-12-29 07:52:11
问题 So I'm designing a sort of my_numeric_cast function to limit the types of conversions available when using a framework I'm writing. It was pretty straight forward to do something like template<typename To, typename From> constexpr To my_numeric_cast(From); template<> constexpr float my_numeric_cast<float, int>(int i) { return i; } Which works, allowing only casting from ints to floats whenever the cast is used. And producing a linkage error whenever a cast not in the white list is attempted.

C++ templates: how to determine if a type is suitable for subclassing

前提是你 提交于 2019-12-29 07:32:10
问题 Let's say I have some templated class depending on type T . T could be almost anything: int , int* , pair <int, int> or struct lol ; it cannot be void , a reference or anything cv-qualified though. For some optimization I need to know if I can subclass T . So, I'd need some trait type is_subclassable , determined as a logical combination of basic traits or through some SFINAE tricks. In the original example, int and int* are not subclassable, while pair <int, int> and struct lol are. EDIT :

Profiling template metaprogram compilation time

萝らか妹 提交于 2019-12-29 04:34:25
问题 I'm working on a C++ project with extensive compile-time computations. Long compilation time is slowing us down. How might I find out the slowest parts of our template meta-programs so I can optimize them? (When we have slow runtime computations, I have many profilers to choose from, e.g. valgrind's callgrind tool. So I tried building a debug GCC and profiling it compiling our code, but I didn't learn much from that.) I use GCC and Clang, but any suggestions are welcome. I found profile

Generating one class member per variadic template argument

☆樱花仙子☆ 提交于 2019-12-28 05:04:05
问题 I have a template class where each template argument stands for one type of value the internal computation can handle. Templates (instead of function overloading) are needed because the values are passed as boost::any and their types are not clear before runtime. To properly cast to the correct types, I would like to have a member list for each variadic argument type, something like this: template<typename ...AcceptedTypes> // e.g. MyClass<T1, T2> class MyClass { std::vector<T1> m

Optimize template replacement of a switch

雨燕双飞 提交于 2019-12-28 03:38:07
问题 I have a lot of custom datatypes in one of my projects which all share a common base class. My data (coming from a database) has a datatype which is distinguished by an enum of the base class. My architecture allows a specific datatype to be specialized with a derived class or it can be handled by the base class. When I construct one my specific datatypes I normally call the constructor directly: Special_Type_X a = Special_Type_X("34.34:fdfh-78"); a.getFoo(); There is some template magic

Is it possible to pass a type and a pointer of that type to a c++ template class using a single parameter of a template class?

流过昼夜 提交于 2019-12-25 01:44:29
问题 Is it possible to pass a type and a pointer of that type to a c++ template class using a single parameter of a template class? I want to take a pointer to a embedded hardware address (an uart) which has the type UART_HandleTypeDef and deduce that type information instead of manually declaring it. Something akin to: template<typename T> class serial{ public: T::value_type* uart = T; }; I want to get away from the normal notation which would require me to state the type and then pass a pointer:

Extending Multi patterned variadic templates in C++

拈花ヽ惹草 提交于 2019-12-24 23:06:39
问题 This question is a follow on from my previous question Multi patterned varadic templates in C++ to which I received the solution: #include <array> #include <iostream> #include <type_traits> template <typename T, std::size_t N> class Vec; template <std::size_t, typename ...> struct dimVec; // ground case for no Vecs: unimplemented for SFINAE failure ! template <> struct dimVec<0U>; // ground case with one or more Vecs: size fixed template <std::size_t N> struct dimVec<N> : public std::integral

How to design a library wrapper in C++?

左心房为你撑大大i 提交于 2019-12-24 12:40:28
问题 I would like to design a wrapper in C++ with a simple syntax: Vector<double,stl> v; // aka std::vector<double> Vector<double, eigen> w; // aka Eigen::VectorXd from Eigen library Matrix<double, eigen> m; // aka Eigen::MatrixXd from Eigen library However, I do not manage to get this syntax, especially for the two last examples. Here is my code for the case of wrapping of STL vectors: #ifndef WRAPPER_HPP #define WRAPPER_HPP #include <cstdlib> #include <vector> //============= // BASE WRAPPER //=

Creating a variant type of all possible map of key-value types at compile time, where the key and value types are specified from a tuple of types

佐手、 提交于 2019-12-24 10:16:38
问题 Currently, I have a variant of map types, where I hard-code all variations of key-value pairs, as such: // for example, if we support std::string and int types as key-value pair using MapCombinator = std::variant< std::map<std::string, std::string>, std::map<std::string, int>, std::map<int, std::string>, std::map<int, int>>; In the real case, I need to support key-value pairs of all fundamental types, in addition to std::string . This is why, I would like to only specify a tuple of types,