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

A quoted text from Foundations of Algorithms in C++11, Volume 1, chap 4, p. 35 : template <typename T> struct allocator { template <typename U> using rebind = allocator<U>; }; sample usage : allocator<int>::rebind<char> x; In The C++ Programming Language, 4th edition, section 34.4.1, p. 998, commenting the ‘classical’ rebind member in default allocator class … Read more

Why does not a template template parameter allow ‘typename’ after the parameter list

Short answer: because the Standard says so. Longer answer: prior to Standardization, C++ templates required the class keyword for all template parameters. However, to stress the fact that templates could also be of non-class (i.e. builtin) type, an alternative keyword typename was introduced. However, in C++98, template-template parameters could only be of class-type, and this … Read more

How to call a templated function if it exists, and something else otherwise?

There are two lookups that are done for the name bar. One is the unqualified lookup at the definition context of foo. The other is argument dependent lookup at each instantiation context (but the result of the lookup at each instantiation context is not allowed to change behavior between two different instantiation contexts). To get … Read more

(Partially) specializing a non-type template parameter of dependent type

See paragraph [temp.class.spec] 14.5.5/8 of the standard: The type of a template parameter corresponding to a specialized non-type argument shall not be dependent on a parameter of the specialization. [ Example: template <class T, T t> struct C {}; template <class T> struct C<T, 1>; // error template< int X, int (*array_ptr)[X] > class A … Read more

C++ template specialization, calling methods on types that could be pointers or references unambiguously

Small overloaded functions can be used to turn reference into pointer: template<typename T> T * ptr(T & obj) { return &obj; } //turn reference into pointer! template<typename T> T * ptr(T * obj) { return obj; } //obj is already pointer, return it! Now instead of doing this: if(elem->Intersects(_bounds) == false) return false; if(elem.Intersects(_bounds) == … Read more

Templates polymorphism

I think the exact terminology for what you need is “template covariance”, meaning that if B inherits from A, then somehow T<B> inherits from T<A>. This is not the case in C++, nor it is with Java and C# generics*. There is a good reason to avoid template covariance: this will simply remove all type … Read more

check if member exists using enable_if

This has become way easier with C++11. template <typename T> struct Model { vector<T> vertices; void transform( Matrix m ) { for(auto &&vertex : vertices) { vertex.pos = m * vertex.pos; modifyNormal(vertex, m, special_()); } } private: struct general_ {}; struct special_ : general_ {}; template<typename> struct int_ { typedef int type; }; template<typename Lhs, … Read more

How to know if a type is a specialization of std::vector?

In C++11 you can also do it in a more generic way: #include <type_traits> #include <iostream> #include <vector> #include <list> template<typename Test, template<typename…> class Ref> struct is_specialization : std::false_type {}; template<template<typename…> class Ref, typename… Args> struct is_specialization<Ref<Args…>, Ref>: std::true_type {}; int main() { typedef std::vector<int> vec; typedef int not_vec; std::cout << is_specialization<vec, std::vector>::value << is_specialization<not_vec, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)