template-meta-programming

Search for an elegant and nonintrusive way to access private methods of a class

这一生的挚爱 提交于 2019-12-12 18:38:34
问题 Disclaimer : This is never meant to be used in production code. It's an exploration at the edges of C++ :) My question is a follow up, based on a discussion with @Johannes Schaub here: calling private methods in c++. I found a very short solution for private member access on his blog: http://bloglitb.blogspot.de/2011/12/access-to-private-members-safer.html Here's a sample: #include <iostream> using namespace std; // example class struct A { A(int a, double b):_a(a),_b(b) { } private: int _a;

Why does gcc complain “error: type 'intT' of template argument '0' depends on a template parameter”?

孤人 提交于 2019-12-12 15:54:10
问题 My compiler is gcc 4.9.0. The following code cannot be compiled: template<typename T, T i> struct value {}; template<typename T> struct value<T, 0> {}; // error: type 'T' of template argument '0' depends on a template parameter What is the cause? and, how to solve this issue? 回答1: GCC is right, this is explicitly forbidden by C++11 [temp.class.spec] §8: 8 Within the argument list of a class template partial specialization, the following restrictions apply: A partially specialized non-type

Mixin's names parameterization with template argument

笑着哭i 提交于 2019-12-12 11:29:02
问题 Is it possible to generate a name for a function within a mixin template? Something like this: mixin template Generator(string name) { @property void mixin(name) pure nothrow // mixin(name) is not valid :( { //some cool stuff here } } 回答1: I'm hoping somebody can come up with something cleaner, but this should do what you want: mixin template Generator(string name) { mixin("alias " ~ name ~ " = _fun;"); @property void _fun pure nothrow { //some cool stuff here } } This unfortunately injects

Replacement for ternary operator in template metaprogramming

自古美人都是妖i 提交于 2019-12-12 10:13:17
问题 I am implementing a binomial coefficient (n choose k) function in C++. Besides using a "normal" function (which is evaluated at runtime) this also can be accomplished using template metaprogramming (when the arguments are known at compile time): template <unsigned int n, unsigned int k> struct Binomialkoeffizient { static const unsigned int value = Binomialkoeffizient<n, k-1>::value * (n-k+1) / k; }; template <unsigned int n> struct Binomialkoeffizient<n, 0> { static const unsigned int value

C++ detect templated class

最后都变了- 提交于 2019-12-12 08:04:10
问题 template<typename T> struct check { static const bool value = false; }; What I want to do is to have check<T>::value be true if and only if T is a std::map<A,B> or std::unordered_map<A,B> and both A and B be std::string . So basically check enables compile-time checking of the type T . How do I do this? 回答1: Partial specialization for when you want to allow any comparator, hasher, key-equal-comparator and allocator: template<class Comp, class Alloc> struct check<std::map<std::string, std:

Template specialization and alias template deduction difference

南笙酒味 提交于 2019-12-12 07:51:24
问题 I'm struggling to understand how deduction works in the following case: template<class Category, Category code> struct AImpl { }; template<class Category, Category code> struct AHelper { using type = AImpl<Category, code>; }; template<class Category, Category code> using A = typename AHelper<Category, code>::type; template<int code> void doSomething(A<int, code> object) { } Following is the test code: A<int, 5> a1; doSomething(a1); // This does not compile doSomething<5>(a1); // This compiles

Is this “Tag Dispatching”?

大兔子大兔子 提交于 2019-12-12 07:36:55
问题 Say I have some code: void barA() { } void barB() { } void fooA() { // Duplicate code... barA(); // More duplicate code... } void fooB() { // Duplicate code... barB(); // More duplicate code... } int main() { fooA(); fooB(); } And I want to remove the duplicate code between fooA and fooB I could use a number of dynamic techniques such as passing in a bool parameter, passing a function pointer or virtual methods but if I wanted a compile time technique I could do something like this: struct A

Compare two sets of types for equality

為{幸葍}努か 提交于 2019-12-12 07:14:37
问题 How can one check if two parameter packs are the same, ignoring their internal order? So far I only have the frame (using std::tuple ), but no functionality. #include <tuple> #include <type_traits> template <typename, typename> struct type_set_eq : std::false_type { }; template <typename ... Types1, typename ... Types2> struct type_set_eq<std::tuple<Types1...>, std::tuple<Types2...>> : std::true_type { // Should only be true_type if the sets of types are equal }; int main() { using t1 = std:

using nested std::array to create an multidimensional array without knowing dimensions or extents until runtime

我的梦境 提交于 2019-12-12 02:25:00
问题 Is it possible for a class to have a member which is a multidimensional array whose dimensions and extents are not known until runtime? I have found (via this guide) a way to create a struct to easily nest std::arrays at compile time using template metaprogramming: #include <array> /* this struct allows for the creation of an n-dimensional array type */ template <typename T,size_t CurrentDimExtent,size_t... NextDimExtent> struct MultiDimArray{ public: //define the type name nestedType to be a

How to find, from which Types is object composed of?

江枫思渺然 提交于 2019-12-11 22:45:15
问题 ok, yesterday I posted almost identical question here , but I wasn't able to modify the answer(working) to my needs... I did not want to mess the other topic, so I have started new one. So, I have 2 (actually about 15) structs, which can composed an object class MyBase{}; template <typename Super, typename T1, typename T2> struct A : public Super { void doStuffA() { cout<<"doing something in A"; } }; template <typename Super, typename T1, typename T2> struct B : public Super { void doStuffB()