template-templates

Variadic template templates and perfect forwarding

懵懂的女人 提交于 2019-11-28 16:30:32
This question on the object generator pattern got me thinking about ways to automate it. Essentially, I want to automate the creation of functions like std::make_pair , std::bind1st and std::mem_fun so that instead of having to write a different function for each template class type, you could write a single variadic template template function that handles all cases at once. Usage of this function would be like: make<std::pair>(1, 2); // equivalent to std::make_pair(1, 2) make<std::binder2nd>(&foo, 3); // equivalent to std::bind2nd(&foo, 3); Is it possible to write this function make ? I have

Template template parameter and default values [duplicate]

孤人 提交于 2019-11-28 01:51:02
This question already has an answer here: Deducing first template argument with other template parameters defaulted 2 answers Consider the following code: template<typename T> struct A { }; // same as A, but with one extra defaulted parameter template<typename T, typename F = int> struct B { }; template<template<typename> typename T> T<int> build() { return {}; } int main() { build<A>(); // works in gcc and clang build<B>(); // works in gcc, does not work in clang } g++ (7.3.0) compiles the code just fine, however, clang++ (5.0.1) emits the following: example.cpp:14:5: error: no matching

Is there any use for named parameters into template template parameters

半世苍凉 提交于 2019-11-27 06:02:16
问题 If I need to define a template foo function with a template-template parameter, I usually do the following: // Notice that the template parameter of class T is unnamed. template <template <typename> class T> void f() { std::cout << "Yay!\n"; } Notice that the template parameter of the template-template parameter is unnamed, but we can assign a name to this parameter: // Now the template parameter of class T is named INNER. template <template <typename INNER> class T> void f(const INNER &inner

Why is allocator::rebind necessary when we have template template parameters?

会有一股神秘感。 提交于 2019-11-26 22:14:30
Every allocator class must have an interface similar to the following: template<class T> class allocator { ... template<class Other> struct rebind { typedef allocator<Other> other; }; }; And classes that use allocators do something redundant like this: template<class T, class Alloc = std::allocator<T> > class vector { ... }; But why is this necessary? In other words, couldn't they have just said: template<class T> class allocator { ... }; template<class T, template<class> class Alloc = std::allocator> class vector { ... }; which is both more elegant, less redundant, and (in some similar

Template template parameter and default values [duplicate]

强颜欢笑 提交于 2019-11-26 18:34:26
问题 This question already has an answer here: Deducing first template argument with other template parameters defaulted 2 answers Consider the following code: template<typename T> struct A { }; // same as A, but with one extra defaulted parameter template<typename T, typename F = int> struct B { }; template<template<typename> typename T> T<int> build() { return {}; } int main() { build<A>(); // works in gcc and clang build<B>(); // works in gcc, does not work in clang } g++ (7.3.0) compiles the

Why is allocator::rebind necessary when we have template template parameters?

大城市里の小女人 提交于 2019-11-26 08:16:10
问题 Every allocator class must have an interface similar to the following: template<class T> class allocator { ... template<class Other> struct rebind { typedef allocator<Other> other; }; }; And classes that use allocators do something redundant like this: template<class T, class Alloc = std::allocator<T> > class vector { ... }; But why is this necessary? In other words, couldn\'t they have just said: template<class T> class allocator { ... }; template<class T, template<class> class Alloc = std:

What are some uses of template template parameters?

眉间皱痕 提交于 2019-11-25 22:47:00
问题 I\'ve seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses does this technique have? 回答1: I think you need to use template template syntax to pass a parameter whose type is a template dependent on another template like this: template <template<class> class H, class S> void f(const H<S> &value) { } Here, H is a template, but I wanted this function to deal with all specializations of