template-templates

Template class that refers to itself as a template template parameter?

可紊 提交于 2019-12-04 04:12:33
This code: template <template <typename> class T> class A { }; template <typename T> class B { A<B> x; }; doesn't compile, I suppose since A<B> is interpreted as A<B<T> > within B 's scope. So, how do you pass B as a template template parameter within it's scope? Try this: template <typename T> class B { A< ::B > x; // fully qualified name for B }; According to C++ Standard 14.6.1/2 you should use the normal name of the template (i.e., the name from the enclosing scope, not the injected-class-name). 来源: https://stackoverflow.com/questions/3052415/template-class-that-refers-to-itself-as-a

C++ variadic template template argument that matches any kind of parameters

試著忘記壹切 提交于 2019-12-02 17:16:26
I was wondering if it's possible to write a template function that can take any other arbitrary template as a parameter and properly match the template name (i.e. not just the resulting class). What I know to work is this: template<template<typename ...> class TemplateT, typename... TemplateP> void f(const TemplateT<TemplateP...>& param); Which will match for instance for f(std::vector<int>()) or f(std::list<int>()) but will not work for f(std::array<int, 3>()) , as the second parameter is a size_t and no type. Now I guess one could do something crazy like: template<template<typename ...>

Is it possible to define an alias for a template-template parameter?

痴心易碎 提交于 2019-12-02 08:49:37
问题 I'm experimenting with template-template for fun. I have the following class: template<template<class> class T, typename R> class Unit { using FullType = T<R>; using Ratio = R; //using Type = T; ... }; I have define type R and T<R> as member-types Ratio and FullType . Is it possible to alias T as Type ? The commented line above give me the following errors on g++ 4.7: expected nested-name-specifier before 'Type' using-declaration for non-member at class scope expected ';' before '=' token

Can a parameter of a template template parameter cause shadowing?

三世轮回 提交于 2019-12-01 17:42:08
Is this legal C++? template <typename T, template <typename T> class> struct S { }; Clang (3.7.1) rejects it, complaining the second T shadows the first T . GCC seems not to care about it and I think that's reasonable. I think it is only the number of parameters that matters in a template template parameter. http://goo.gl/51bHVG (gcc.godbolt.org) No. [temp.local]/6 : A template-parameter shall not be redeclared within its scope (including nested scopes). 来源: https://stackoverflow.com/questions/35744299/can-a-parameter-of-a-template-template-parameter-cause-shadowing

Can a parameter of a template template parameter cause shadowing?

人走茶凉 提交于 2019-12-01 15:43:28
问题 Is this legal C++? template <typename T, template <typename T> class> struct S { }; Clang (3.7.1) rejects it, complaining the second T shadows the first T . GCC seems not to care about it and I think that's reasonable. I think it is only the number of parameters that matters in a template template parameter. http://goo.gl/51bHVG (gcc.godbolt.org) 回答1: No. [temp.local]/6: A template-parameter shall not be redeclared within its scope (including nested scopes). 来源: https://stackoverflow.com

Use std::tuple for template parameter list instead of list of types

守給你的承諾、 提交于 2019-12-01 06:50:54
I'm trying to make a call to a templated function like this : typedef std::tuple<int, double, bool> InstrumentTuple; Cache cache; InstrumentTuple tuple = cache.get<InstrumentTuple>(); I know I could "simply" pass the types of the tuple. This is what I do know but it is quite cumbersome since I make a lot of calls to this function and since the tuples are quite long: InstrumentTuple tuple = c.get<int, double, bool>(); // syntax I'd like to avoid So I tried multiple implementations of the get method, but with no success : Enabling via a template parameter #include <tuple> class Cache { private:

Template template parameters and variadic templates with gcc 4.4

女生的网名这么多〃 提交于 2019-12-01 03:19:55
I'm using gcc 4.4 on Debian squeeze. Consider the following code. #include <map> #include <string> using std::map; using std::string; // Args lets the user specify additional explicit template arguments template <typename T, template <typename T, typename... Args> class C, typename... Args> C<T, Args...> foo() { C<T, Args...> x; return x; } int main(void) { map<string, int> a = foo<string, map, int>(); } So, the idea here is that T matches string , C matches map , and the template parameter pack Args matches int . I may have some of the syntax wrong, please correct if so. In particular, if one

Template template parameters and variadic templates with gcc 4.4

狂风中的少年 提交于 2019-11-30 23:25:23
问题 I'm using gcc 4.4 on Debian squeeze. Consider the following code. #include <map> #include <string> using std::map; using std::string; // Args lets the user specify additional explicit template arguments template <typename T, template <typename T, typename... Args> class C, typename... Args> C<T, Args...> foo() { C<T, Args...> x; return x; } int main(void) { map<string, int> a = foo<string, map, int>(); } So, the idea here is that T matches string , C matches map , and the template parameter

Template template parameters with variadic templates

半世苍凉 提交于 2019-11-30 19:24:47
问题 For the sake of clarity, I've removed things like the constructor & destructor etc from the below where they don't add anything to the question. I have a base class that is used to create a common ancestor for a derived template class. class PeripheralSystemBase { public: virtual void someFunctionThatsCommonToAllPeripherals() {} }; template <class T, uint32_t numPeripherals = 1> class PeripheralSystem : public PeripheralSystemBase { public: PeripheralSystem() : vec(T) {} std::vector<T> vec; /

C++11 template alias as template template argument leads to different type?

本小妞迷上赌 提交于 2019-11-29 13:27:23
We have observed a strange behaviour in the compilation of the follwing source code: template<template<class> class TT> struct X { }; template<class> struct Y { }; template<class T> using Z = Y<T>; int main() { X<Y> y; X<Z> z; z = y; // it fails here } This is a slightly modified example taken from the c++11 standard proposal for template aliases: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf (See page 4) Also note that the proposal "declares y and z to be of the same type." In our interpretation it should therefore be possible to assign (or copy construct) z from y.