template-meta-programming

How to pass other template parameter when template class uses parameter pack?

為{幸葍}努か 提交于 2019-12-11 12:12:36
问题 I would like to create template class that implements print() method for each type passed as template parameters. Something like that: class Interface { public: virtual ~Interface() = default; virtual void print(int) = 0; virtual void print(double) = 0; }; X x<int, double, Interface>; class X has public method void print() and it works. The whole code below: #include <iostream> #include <type_traits> struct Printer { void print(int i) {std::cout << i << std::endl; } void print(double d) {std:

Partial class specialization for function pointer type and value

时光毁灭记忆、已成空白 提交于 2019-12-11 10:40:57
问题 I'm using FLTK to do my GUI related stuff, and it requires functions of type void (*fn)( Fl_Widget*, void* ) to be registered as widget callbacks. I'm tired of creating function forwarders by hand that unpack the void* s into parameters and call appropriate static functions from my classes to do the work the user has requested. I came up with a solution, but it requires a class to be specialized for both function type, and function address, and this is where I'm having problems. Here's some

Storing States in C++ Metaprogramming?

℡╲_俬逩灬. 提交于 2019-12-11 07:44:18
问题 Is it possible to store states in C++ metaprograms? I'm not entirely sure what I mean, but I want a template class that can be "assigned" to, e.g. state::value can be changed via the use of C++ templates. I know I probably sound very vague, but I hope someone understands what I'm talking about (also please no Boost.MPL). 回答1: It's not possible, the TMP part of C++ is a purely functional language without the concept of side effects. 回答2: That would be a heaven for bugs. It would also violate

C++ Template Meta Programming: Different Behavior using Types Aliases vs Inheritance

时光总嘲笑我的痴心妄想 提交于 2019-12-11 04:55:25
问题 I was trying to reverse a c++14 std::index_sequence and ran into problems with my original implementation that used inheritance. I found a workaround using local type aliases, but I would like to understand why the original code does not work. Broken Reverse Using Inheritance This was my first attempt at reversing a std::index_sequence : /// Helper class that appends an element onto an index_sequence. /// Base case. template<size_t, typename> struct Append : std::index_sequence<> { };

Detecting void method in c++ template metaprogramming

二次信任 提交于 2019-12-11 04:49:24
问题 I am trying to write a template metafunction to detect if a type has a member function with void type. Currently I am able to use SFINAE to detect if a member function has a definite type such as double, int, etc. using something like template<typename C> static auto Test(void*) -> decltype(int{std::declval<C>().foo()}, Yes{}); And of course I can invert that (as shown in the attached code snippet) to test that it is not int, but I am unable to figure out how to test that it is void. The code

Building a list of types during compile time - no C++11

做~自己de王妃 提交于 2019-12-11 04:25:56
问题 I'd like to do exact this to get a list of types/classes. But I cannot use C++11. Any suggestion how I can append type to the template list? Edit: some code to what I'd like to do: #include <iostream> #include <typeinfo> #include <boost/mpl/vector.hpp> #include <boost/mpl/push_back.hpp> #include <boost/mpl/for_each.hpp> using namespace std; class A {}; class B {}; class C {}; typedef boost::mpl::vector<> type1; // supposed I'd like to have this as a #define macro so someone can call //

fill static templated arrays with metaprogramming and variadic templates

梦想的初衷 提交于 2019-12-11 02:53:10
问题 I know that there are easier ways to do it, but I would like to initialize at compilation time the map from unrolled index of 2d array to its general format. I would like to do this without needing to instansiate the array object. Below I define the map from array[][]->array[] . Now I wonder how to do the opposite: [] -> [][] without hardcoding the chosen mapping scheme. I guess that should be possible using metaprogramming and variadic templates. But I tried using it for the first time just

Boost Fusion: validate adapted struct member ordering at compile time

喜夏-厌秋 提交于 2019-12-11 02:16:48
问题 I'm using BOOST_FUSION_ADAPT_STRUCT() , and I need to check that all the members are declared and in the correct order. So first I did this: template <typename Sequence> struct checker { static void check() { typedef typename mpl::accumulate<Sequence, mpl::size_t<0>, mpl::plus<mpl::_1, mpl::sizeof_<mpl::_2>>>::type total_size; static_assert(sizeof(Sequence) == total_size::value, "omitted field?"); } }; And this works: struct foo { int x; float y; double z; }; BOOST_FUSION_ADAPT_STRUCT(foo, x,

Get the size of multi dimensional std::array at compile time via constexpr or template function

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 23:21:11
问题 I use a three dimensional std::array , because the size is already known at compile. However, I noticed that the size() function is not static, and thus, inaccessible for constexpr/template functions. I already found the demo example below, which estimates the size of a one dimensional std::array . However, this does not work for two or more dimensions. Is there a way to return the other dimensions by writing a function with an additional template parameter dim for the x, y, z, .. dimension?

Template argument calculation at compile time

柔情痞子 提交于 2019-12-10 18:43:37
问题 I'm trying to deduce the greater of two template arguments at compile time. Both template arguments are of type size_t. I have a templated type, SomeType, which takes a size_t as it's template argument. I then have a function that takes two SomeType parameters with different template size_t's and i want the return type to be a SomeType with its templated size_t to be the greater of the two input size_t sizes. template <size_t d> struct SomeType {...} template<size_t d1, size_t d2> SomeType