templates

Function template overload resolution, dependent and non-dependent parameters

一曲冷凌霜 提交于 2021-02-07 02:07:22
问题 Given the following program #include <iostream> template<class T> struct id { using type = T; }; template<class T1, class T2> int func(T1, T2) { return 0; } template<class T1, class T2> int func(typename id<T1>::type, typename id<T2>::type) { return 1; } int main() { std::cout << func<int, int>(0, 0) << std::endl; } GCC and Clang both prints 1 for this program. Is this program guaranteed to print 1 by the standard? I tried finding the answer here but couldn't decipher it. It looks like the

When to prefer templated policy based design over non-templated inheritance based design

无人久伴 提交于 2021-02-06 20:43:03
问题 I am trying to understand the real requirement of the usage of templates for policy based design. Going through the new templated designs in C++ I found that policy based class design is a highly suggested way of design which allows you to 'plug-in' different behaviors from policy classes. A minimal example is the following (a shortened version of the wiki): template <typename LanguagePolicy> class HelloWorld : private LanguagePolicy { using LanguagePolicy::message; public: // Behaviour

How to check for the existence of a subscript operator?

南笙酒味 提交于 2021-02-06 18:47:07
问题 I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression is possible but does not work when the bracket operator does not exist. #include <iostream> #include <vector> #include <cassert> template<class T, class Index> struct has_subscript_operator_impl { template<class T1, class Reference = decltype( (*std::declval<T*>())[std::declval<Index>()] ), class = typename std::enable

How to check for the existence of a subscript operator?

落爺英雄遲暮 提交于 2021-02-06 18:44:43
问题 I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression is possible but does not work when the bracket operator does not exist. #include <iostream> #include <vector> #include <cassert> template<class T, class Index> struct has_subscript_operator_impl { template<class T1, class Reference = decltype( (*std::declval<T*>())[std::declval<Index>()] ), class = typename std::enable

How to check for the existence of a subscript operator?

早过忘川 提交于 2021-02-06 18:40:20
问题 I want to write a type trait which uses SFINAE to check a type for the existence of a subscript expression. My initial attempt below seems to work when the subscript expression is possible but does not work when the bracket operator does not exist. #include <iostream> #include <vector> #include <cassert> template<class T, class Index> struct has_subscript_operator_impl { template<class T1, class Reference = decltype( (*std::declval<T*>())[std::declval<Index>()] ), class = typename std::enable

Curiously mutually recurring class definitions

梦想的初衷 提交于 2021-02-06 15:01:40
问题 I want type declarations in two classes to mutually depend on each other. Here is a first example that compiles both with clang and gcc: template <class Sum> struct A { using X = char; // (1) using Z = typename Sum::B::Y; // (2) }; template <class Sum> struct B { using Y = typename Sum::A::X; }; struct AplusB { using A = ::A<AplusB>; using B = ::B<AplusB>; }; AplusB::A::Z z; int main() {} There is an interesting moment, however. If you swap lines (1) and (2), then it will fail to compile with

How to call constructor of a template base class in a template derived class?

爷,独闯天下 提交于 2021-02-06 09:09:45
问题 Let's say I have a base template class Array: template <class T = Point> class Array{ protected: T* m_data; int size; public: Array(); // constructor Array(int n); // constructor Array(const Array<T>& s_data); //Copy Constructor // and so on.. } And it has constructors and destructors. Also I have a derived template class NumericArray: template <class T = int> class NumericArray:public Array<T>{ public: NumericArray(); NumericArray(int n); NumericArray(const NumericArray<T>& s_data);

How to call constructor of a template base class in a template derived class?

末鹿安然 提交于 2021-02-06 09:04:29
问题 Let's say I have a base template class Array: template <class T = Point> class Array{ protected: T* m_data; int size; public: Array(); // constructor Array(int n); // constructor Array(const Array<T>& s_data); //Copy Constructor // and so on.. } And it has constructors and destructors. Also I have a derived template class NumericArray: template <class T = int> class NumericArray:public Array<T>{ public: NumericArray(); NumericArray(int n); NumericArray(const NumericArray<T>& s_data);

How to call constructor of a template base class in a template derived class?

亡梦爱人 提交于 2021-02-06 09:04:29
问题 Let's say I have a base template class Array: template <class T = Point> class Array{ protected: T* m_data; int size; public: Array(); // constructor Array(int n); // constructor Array(const Array<T>& s_data); //Copy Constructor // and so on.. } And it has constructors and destructors. Also I have a derived template class NumericArray: template <class T = int> class NumericArray:public Array<T>{ public: NumericArray(); NumericArray(int n); NumericArray(const NumericArray<T>& s_data);

template template parameter expansion for variadic templates

丶灬走出姿态 提交于 2021-02-06 08:56:15
问题 I recently learned about the existence of template template parameters and was now wondering if something like this would be possible: template<template<class... > class Container, typename... args> struct ContainerTemplate { using container = std::tuple<Container<args...>...>; }; what i want is a template that gets a Container or some other template class as a template template parameter and then expands the rest of the template arguments in such a way that if Container has N template args