Overloading multiple function objects by reference

All right, here’s the plan: we’re going to determine which function object contains the operator() overload that would be chosen if we used a bare-bones overloader based on inheritance and using declarations, as illustrated in the question. We’re going to do that (in an unevaluated context) by forcing an ambiguity in the derived-to-base conversion for … Read more

Generating one class member per variadic template argument

As you have already been hinted, the best way is to use a tuple: template<typename …AcceptedTypes> // e.g. MyClass<T1, T2> class MyClass { std::tuple<std::vector<AcceptedTypes>…> vectors; }; This is the only way to multiply the “fields” because you cannot magically make it spell up the field names. Another important thing may be to get some named … Read more

Why do C++ templates use the angle bracket syntax?

Templates were introduced in the 1988 USENIX paper Parameterized Types for C++ by Bjarne Stroustrup, later incorporated into The Annotated C++ Reference Manual published in 1990 (the version before standardized C++). According to the paper, The <…> brackets are used in preference to the parentheses (…) partly to emphasize the different nature of template arguments … Read more

How can I get the depth of a multidimensional std::vector at compile time?

A classic templating problem. Here’s a simple solution like how the C++ standard library does. The basic idea is to have a recursive template that will count one by one each dimension, with a base case of 0 for any type that is not a vector. #include <vector> #include <type_traits> template<typename T> struct dimensions : … Read more

Compile-time constant id

This is sufficient assuming a standards conforming compiler (with respect to the one definition rule): template<typename T> class A { public: static char ID_storage; static const void * const ID; }; template<typename T> char A<T>::ID_storage; template<typename T> const void * const A<T>::ID= &A<T>::ID_storage; From the C++ standard 3.2.5 One definition rule [basic.def.odr] (bold emphasis mine): … Read more

Automatically pick a variable type big enough to hold a specified number

Boost.Integer already has facilities for Integer Type Selection: boost::int_max_value_t<V>::least The smallest, built-in, signed integral type that can hold all the values in the inclusive range 0 – V. The parameter should be a positive number. boost::uint_value_t<V>::least The smallest, built-in, unsigned integral type that can hold all positive values up to and including V. The parameter … Read more

What is the difference between a trait and a policy?

Policies Policies are classes (or class templates) to inject behavior into a parent class, typically through inheritance. Through decomposing a parent interface into orthogonal (independent) dimensions, policy classes form the building blocks of more complex interfaces. An often seen pattern is to supply policies as user-definable template (or template-template) parameters with a library-supplied default. An … Read more

void_t “can implement concepts”?

Yes, concepts lite basically dresses up SFINAE. Plus it allows deeper introspection to allow for better overloading. However that only works if the concept predicates are defined as concept bool. The improved overloading does not work with the current concept predicates, but conditional overloading can be used. Lets look how we can define predicates, constrain … Read more

How to make generic computations over heterogeneous argument packs of a variadic template function?

Since I was not happy with what I found, I tried to work out a solution myself and ended up writing a small library which allows formulating generic operations on argument packs. My solution has the following features: Allows iterating over all or some elements of an argument pack, possibly specified by computing their indices … Read more

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

I’m not so sure I get the full extent of the question, but here are a few hints The line commented with // THIS is what I need to do. compiles fine with me (problem solved? I’m guessing you actually meant assigning a parser, not a rule?) Initialization of function-local static has been defined to … Read more

tech