template-meta-programming

How to build a compile-time key/value store?

痴心易碎 提交于 2019-12-03 02:21:10
I have a problem where I need to map an integer at compile time to another integer. Basically, I need the compile-time equivalent of std::map<int,int> . If a key is not found in the map, I'd like to return a default value. The interface I'd like to use: template<unsigned int default_value, unsigned int key0, unsigned int value0, unsigned int key1, unsigned int value1, ...> struct static_map { ... }; template<unsigned int key, typename StaticMap> struct lookup { static unsigned int value = ... }; lookup returns the value associated with key in the StaticMap . If key is not found, then default

How do I use std::enable_if with a self-deducing return type?

左心房为你撑大大i 提交于 2019-12-03 02:15:29
C++14 will have functions whose return type can be deduced based on the return value. auto function(){ return "hello world"; } Can I apply this behaviour to functions that use enable_if for the SFINAE by return type idiom? For example, let's consider the following two functons: #include <type_traits> #include <iostream> //This function is chosen when an integral type is passed in template<class T > auto function(T t) -> typename std::enable_if<std::is_integral<T>::value>::type { std::cout << "integral" << std::endl; return; } //This function is chosen when a floating point type is passed in

Can I overload functions with type-traits?

守給你的承諾、 提交于 2019-12-03 02:12:10
Let's say, I have six types, and they each belong in a conceptual category. Here is a diagram that shows this: Or Perhaps a more specific example for you: I want to write two functions that will handle all 6 types. Types in "Category 1" get handled a certain way, and types in "Category 2" get handled a different way. Let's get into the code. First, I'll create the six types. //Category 1 Types class Type_A{}; class Type_B{}; class Type_C{}; //Category 2 Types class Type_D{}; class Type_E{}; class Type_F{}; Next, I'll create two type traits so that the category of the type can be discovered at

What is the difference between a trait and a policy?

你。 提交于 2019-12-03 01:35:56
问题 I have a class whose behavior I am trying to configure. template<int ModeT, bool IsAsync, bool IsReentrant> ServerTraits; Then later on I have my server object itself: template<typename TraitsT> class Server {...}; My question is for my usage above is my naming misnamed? Is my templated parameter actually a policy instead of a trait? When is a templated argument a trait versus a policy? 回答1: Policies Policies are classes (or class templates) to inject behavior into a parent class, typically

Overloading multiple function objects by reference

非 Y 不嫁゛ 提交于 2019-12-03 01:02:44
问题 In C++17, it is trivial to implement an overload(fs...) function that, given any number of arguments fs... satisfying FunctionObject, returns a new function object that behaves like an overload of fs... . Example: template <typename... Ts> struct overloader : Ts... { template <typename... TArgs> overloader(TArgs&&... xs) : Ts{forward<TArgs>(xs)}... { } using Ts::operator()...; }; template <typename... Ts> auto overload(Ts&&... xs) { return overloader<decay_t<Ts>...>{forward<Ts>(xs)...}; } int

How do I determine if a type is callable with only const references?

亡梦爱人 提交于 2019-12-02 23:59:30
I want to write a C++ metafunction is_callable<F, Arg> that defines value to be true , if and only if the type F has the function call operator of the form SomeReturnType operator()(const Arg &) . For example, in the following case struct foo { void operator(const int &) {} }; I want is_callable<foo, int &> to be false and is_callable<foo, const int &> to be true . This is what I have so far : #include <memory> #include <iostream> template<typename F, typename Arg> struct is_callable { private: template<typename> static char (&test(...))[2]; template<unsigned> struct helper { typedef void

more spirit madness - parser-types (rules vs int_parser<>) and meta-programming techniques

馋奶兔 提交于 2019-12-02 21:32:14
The question is in bold at the bottom, the problem is also summarized by the distillation code fragment towards the end. I am trying to unify my type system (the type system does to and from from type to string) into a single component(as defined by Lakos). I am using boost::array , boost::variant , and boost::mpl , in order to achieve this. I want to have the parser and generator rules for my types unified in a variant. there is a undefined type, a int4(see below) type and a int8 type. The variant reads as variant<undefined, int4,int8> . int4 traits: struct rbl_int4_parser_rule_definition {

What are the coolest examples of metaprogramming that you've seen in C++? [closed]

旧街凉风 提交于 2019-12-02 20:54:05
What are the coolest examples of metaprogramming that you've seen in C++? What are some practical uses of metaprogramming that you've seen in C++? Ferruccio Personally, I think Boost.Spirit is a pretty amazing example of meta-programming. It's a complete parser generator that lets you express grammars using C++ syntax. JaredPar The most practical use of meta programming is turning a runtime error into a compile time error. Example: Lets call the interface IFoo. One of my programs dealt with a COM object that had multiple paths to IFoo (very complicated inheritance hierarchy). Unfortunately the

Variadic templates and switch statement?

我怕爱的太早我们不能终老 提交于 2019-12-02 19:11:47
I have the following function which can take N arguments of different types, and forwards them to N functions templated on each individual type, in this manner (example with two arguments): template <typename T1, typename T2> bool func(int& counter, T1 x1, T2 x2) { switch (counter) { case 0: if (func2<T1>(x1)) { counter++; return true; } else { return false; } case 1: if (func2<T2>(x2)) { counter++; return true; } else { return false; } default: return true; } } I want to write this function with variadic templates so that it can handle any number of arguments in a type-safe way. I can see a

partial type as template argument c++ [duplicate]

喜欢而已 提交于 2019-12-02 16:40:17
问题 This question already has answers here : What are some uses of template template parameters? (10 answers) Closed 5 years ago . Simply, can I pass std::vector as a template argument. Following example list usage tempate<typename container_t, typename value_t> struct container_types { typedef container_t<value_t> value_container_t; typedef container_t<pair<value_t> pair_container_t; }; I wish to pass container_types to generate final two data types. If this is not feasible, then how can I