template-meta-programming

Is it possible to invoke a method with all possible K-combinations (with repetition) of arguments passed in a tuple?

邮差的信 提交于 2019-12-01 22:22:11
The desired behaviour can be illustrated as follows: void foo(int x, int y) { std::cout << x << " " << y << std::endl; } int main() { all_combinations<2>(foo, std::make_tuple(1, 2)); // K = 2 // to run: // foo(1, 1) // foo(1, 2) // foo(2, 1) // foo(2, 2) } The c++14 version could look as follows: #include <tuple> #include <utility> #include <iostream> #include <initializer_list> template <class Foo, class Tuple, size_t... Is, size_t... Is2> int all_combinations_impl(Foo foo, Tuple t, std::index_sequence<Is...> , std::integral_constant<size_t, 0>, std::index_sequence<Is2...>) { foo(std::get<Is>

C++ template for unrolling a loop using a switch?

为君一笑 提交于 2019-12-01 20:20:55
问题 My question is similar to Can one unroll a loop when working with an integer template parameter? but I want to mix compile time and runtime. Specifically, I know at compile time a constant NBLOCK and I want to write a switch on a variable start_block which is only known at runtime where NBLOCK is the number of entries in the switch. Here is what I got using macros: #define CASE_UNROLL(i_loop) \ case i_loop : \ dst.blocks[i_loop+1] -= (load_unaligned_epi8(srcblock) != zero) & block1; \

Find number of unique values of a parameter pack

自闭症网瘾萝莉.ら 提交于 2019-12-01 19:18:06
Given a parameter pack with variadic arguments, how can one find the number of unique values in the pack. I am looking for something along the lines of no_of_uniques<0,1,2,1,2,2>::value // should return 3 My rudimentary implementation looks something this template <size_t ... all> struct no_of_uniques; // this specialisation exceeds -ftemplate-depth as it has no terminating condition template <size_t one, size_t ... all> struct no_of_uniques<one,all...> { static const size_t value = no_of_uniques<one,all...>::value; }; template <size_t one, size_t two, size_t three> struct no_of_uniques<one

C++ template for unrolling a loop using a switch?

我们两清 提交于 2019-12-01 19:12:00
My question is similar to Can one unroll a loop when working with an integer template parameter? but I want to mix compile time and runtime. Specifically, I know at compile time a constant NBLOCK and I want to write a switch on a variable start_block which is only known at runtime where NBLOCK is the number of entries in the switch. Here is what I got using macros: #define CASE_UNROLL(i_loop) \ case i_loop : \ dst.blocks[i_loop+1] -= (load_unaligned_epi8(srcblock) != zero) & block1; \ srcblock += sizeof(*srcblock); switch(start_block) { CASE_UNROLL(0); #if NBLOCKS > 2 CASE_UNROLL(1); #endif

Template parameters in C++ templates

对着背影说爱祢 提交于 2019-12-01 17:34:55
I am trying to use template template parameters, similar to what is done here and here (and many other places). #include <vector> template<template<class> class A, class B> void f(A<B> &value) { } int main() { std::vector<int> value; f<std::vector, int>(value); } But $ g++-4.8 -std=c++0x base64.cpp base64.cpp: In function ‘int main()’: base64.cpp:9:23: error: no matching function for call to ‘f(std::vector<int>&)’ f<std::vector, int>(value); ^ base64.cpp:9:23: note: candidate is: base64.cpp:4:6: note: template<template<class> class H, class S> void f(const H<S>&) void f(H<S> &value) { What am

Template parameters in C++ templates

ぃ、小莉子 提交于 2019-12-01 17:05:33
问题 I am trying to use template template parameters, similar to what is done here and here (and many other places). #include <vector> template<template<class> class A, class B> void f(A<B> &value) { } int main() { std::vector<int> value; f<std::vector, int>(value); } But $ g++-4.8 -std=c++0x base64.cpp base64.cpp: In function ‘int main()’: base64.cpp:9:23: error: no matching function for call to ‘f(std::vector<int>&)’ f<std::vector, int>(value); ^ base64.cpp:9:23: note: candidate is: base64.cpp:4

How to check whether T is an aggregate type?

孤者浪人 提交于 2019-12-01 17:04:44
I know about std::is_pod . But it checks more than just aggregate types. Or, is std::is_pod just the best we can do? Basically, I want to write a function template for this : template <typename T> aggregate_wrapper<T> wrap(T&& x); which is only enabled when T is an aggregate type. Nicol Bolas There is no way to synthesize an is_aggregate template. The rules for whether something participates in aggregate initialization cannot be detected by C++14 metaprogramming techniques (they would require reflection support). The general reason for not having this is the lack of an explicit need. Even in

Why are type_traits implemented with specialized template structs instead of constexpr?

烈酒焚心 提交于 2019-12-01 16:25:54
Is there any reason why the standard specifies them as template struct s instead of simple boolean constexpr ? In an additional question that will probably be answered in a good answer to the main question, how would one do enable_if stuff with the non-struct versions? One reason is that constexpr functions can't provide a nested type member, which is useful in some meta-programming situations. To make it clear, I'm not talking only of transformation traits (like make_unsigned ) that produce types and obviously can't be made constexpr functions. All type traits provide such a nested type

finding type, for which is_constructible holds

不想你离开。 提交于 2019-12-01 08:41:59
I was playing around with templates and was trying to implement following helper. first_constructible<Types..., Args...>::type which would return first type of Types which is constructible from Args... . First problem obviously is having two parameter packs in struct , so I changed usage to first_constructible<std::tuple<Types...>, Args...>::type I've implemented it by splitting tuple types as first and rest, checked using std::is_constructible and recursed if neccessary. template<typename T> struct pop_front_tuple { template<typename U, typename... Us> static std::tuple<Us...> impl(std::tuple

Checking whether a function (not a method) exists in c++11 via templates

耗尽温柔 提交于 2019-12-01 08:03:56
So with SFINAE and c++11, it is possible to implement two different template functions based on whether one of the template parameters can be substituted. For example struct Boo{ void saySomething(){ cout << "Boo!" << endl; } }; template<class X> void makeitdosomething(decltype(&X::saySomething), X x){ x.saySomething(); } template<class X> void makeitsaysomething(int whatever, X x){ cout << "It can't say anything!" << endl; } int main(){ makeitsaysomething(3); makeitsaysomething(Boo()); } or something along that line. My question is.. how does one do the same thing, but for non-member