template-meta-programming

How to make static_assert play nice with SFINAE

会有一股神秘感。 提交于 2019-12-05 08:23:17
Update I posted a working rough draft of rebind as an answer to the question. Though I didn't have much luck finding a generic way to keep static_assert s from breaking metafunctions. Basically I want to check if a templated type T<U, Args...> can be constructed from some other type T<V, Args...> . Where T and Args... is the same in both types. The problem is, T<> might have a static_assert in it that totally breaks my metafunction. Below is a rough summary of what I'm trying to do. template<typename T> struct fake_alloc { using value_type = T; }; template<typename T, typename Alloc = fake

constexpr function not calculate value in compile time

旧街凉风 提交于 2019-12-05 06:39:18
I want to compare meta programming and use of constexpr in c++0x. then I write a fib function in both model. when I use meta programming model, answer print out very fast because it calculated in compile time. but when I use constexpr funcion it calculate value in run time, not in compile time. I using g++( gcc ) 4.8 .can any body help me? #include <iostream> using namespace std; #define NUM 42 template <unsigned int N> struct Fibonacci { enum { value = Fibonacci<N - 1>::value + Fibonacci<N - 2>::value }; }; template <> struct Fibonacci<1> { enum { value = 1 }; }; template <> struct Fibonacci

C++14 Metaprogramming: Automagically build a list of types at compile / init time

主宰稳场 提交于 2019-12-05 06:11:51
Using C++14 and some combination of the Curiously Recurring Template Pattern (CRTP) and possibly Boost.Hana (or boost::mpl if you wish), can I build a list of types at compile time (or static initialization time) without an explicit declaration? As an example, I have something like this (see it on Coliru ): #include <iostream> #include <boost/hana/tuple.hpp> #include <boost/hana/for_each.hpp> namespace { struct D1 { static constexpr auto val = 10; }; struct D2 { static constexpr auto val = 20; }; struct D3 { static constexpr auto val = 30; }; } int main() { // How to avoid explicitly defining

Hashing types at compile-time in C++17/C++2a

半腔热情 提交于 2019-12-05 04:29:12
Consider the following code: #include <iostream> #include <type_traits> template <class T> constexpr std::size_t type_hash(T) noexcept { // Compute a hash for the type // DO SOMETHING SMART HERE } int main(int argc, char* argv[]) { auto x = []{}; auto y = []{}; auto z = x; std::cout << std::is_same_v<decltype(x), decltype(y)> << std::endl; // 0 std::cout << std::is_same_v<decltype(x), decltype(z)> << std::endl; // 1 constexpr std::size_t xhash = type_hash(x); constexpr std::size_t yhash = type_hash(y); constexpr std::size_t zhash = type_hash(z); std::cout << (xhash == yhash) << std::endl; //

How do I determine the number of parameters of a std::function?

若如初见. 提交于 2019-12-05 04:04:49
问题 I have the following problem. Say you want to write a generic function that can take a lambda expression. I understand that if the parameter is of type std::function, then I could not only use lambdas, but also functions and even pointers to functions. So at a first step, I did the following: void print(std::function<void(int, int)> fn) { fn(1,2); } int main() { print([](int i, int j) { std::cout << j <<','<<i<<'\n'; }); return 0; } Now the problem is that I want to make this function generic

Detect if C++ lambda can be converted to function pointer

我们两清 提交于 2019-12-05 03:28:17
I have some code that generates assembly for a JIT idea I'm working on. I use meta-programming to generate calls by analyzing the function type and then generating the correct assembly to call it. I recently wanted to add lambda support, and lambdas have two versions, non-capturing (normal __cdecl function call) and capturing (__thiscall, member-function call with the lambda object as context). __thiscall is slightly more expensive so I'd like to avoid it whenever possible, and I'd also like to avoid having to use different call generation functions depending on the lambda type. I tried many

How to extract the highest-indexed specialization from a structure?

亡梦爱人 提交于 2019-12-05 01:33:39
I'm trying to do some template metaprogramming and I'm finding the need to "extract" the highest index of a specialization of some structure in some type. For example, if I have some types: struct A { template<unsigned int> struct D; template<> struct D<0> { }; }; struct B { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; }; struct C { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; template<> struct D<2> { }; }; How can I then write a metafunction like this: template<class T> struct highest_index { typedef ???

C++ type traits to select between T1 and T2

帅比萌擦擦* 提交于 2019-12-05 01:02:57
I want a template to select from two types based on some condition. E.g. struct Base {}; template <typename T1, typename T2> struct test { // e.g. here it should select T1/T2 that is_base_of<Base> typename select_base<T1, T2>::type m_ValueOfBaseType; }; Of course to pass condition to the select_base (to make it generic) would be useful, but hard-coded solution is easier and good as well. Here's a sample solution that I tried but it always selects T1: http://ideone.com/EnVT8 The question is how to implement the select_base template. Matthieu M. C++14 (and onwards): template <typename T,

Compute nth prime at compile time [closed]

匆匆过客 提交于 2019-12-05 01:02:55
问题 This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 6 years ago . The C++11 features, with constexpr and template argument packs, should in my opinion be strong enough to perform some rather complex computations. One

Ensure template parameter is an enum class [duplicate]

*爱你&永不变心* 提交于 2019-12-04 22:31:26
This question already has an answer here: Is it possible to determine if a type is a scoped enumeration type? 1 answer Is there a way to ensure a template parameter is an enum-class type? I know type_traits has std::is_enum , but I don't want it to match regular enums, just enum_classes. Example of the wanted effect: enum class EnumClass {}; enum Enum {}; class Class {}; template <typename T> void Example() { static_assert(/* T is EnumClass */, "`T` must be an enum class"); } Example<EnumClass>(); // Ok Example<Enum>(); // Error Example<Class>(); // Error I am using C++11, and unfortunately