class-template

C++ - Template Specialization & Partial Specialization

萝らか妹 提交于 2019-12-05 02:00:21
问题 I have been looking all over the internet and stackoverflow for a concrete answer but I can't seem to find one. I have to create a generic class and then implement specific functions. My specific instructions were: You need to make use of Template Expression Parameters and Template Class Specialization and Partial Specialization. I have a template class: template <class T, int x, int y> class Z { T **array[x][y]; public: Z(); void print(); //and other methods }; I need to: 1) Only Z's where x

C++ - Template Specialization & Partial Specialization

谁都会走 提交于 2019-12-03 16:25:12
I have been looking all over the internet and stackoverflow for a concrete answer but I can't seem to find one. I have to create a generic class and then implement specific functions. My specific instructions were: You need to make use of Template Expression Parameters and Template Class Specialization and Partial Specialization. I have a template class: template <class T, int x, int y> class Z { T **array[x][y]; public: Z(); void print(); //and other methods }; I need to: 1) Only Z's where x= 2 and y = 2 need to have a public method void J() 2) For char Z's of x = 2 and y= 2 J will do

Nested template argument deduction for class templates not working

家住魔仙堡 提交于 2019-12-03 16:20:51
问题 In this Q&A I wrote a little wrapper class that provides reverse iterator access to a range, relying on the c++1z language feature template argument deduction for class templates (p0091r3, p0512r0) #include <iostream> #include <iterator> #include <vector> template<class Rng> class Reverse { Rng const& rng; public: Reverse(Rng const& r) noexcept : rng(r) {} auto begin() const noexcept { using std::end; return std::make_reverse_iterator(end(rng)); } auto end() const noexcept { using std::begin;

Why is this call to swap() ambiguous?

被刻印的时光 ゝ 提交于 2019-12-03 10:39:37
The following program #include <algorithm> #include <utility> #include <memory> namespace my_namespace { template<class T> void swap(T& a, T& b) { T tmp = std::move(a); a = std::move(b); b = std::move(tmp); } template<class T, class Alloc = std::allocator<T>> class foo {}; } int main() { my_namespace::foo<int> *a, *b; using my_namespace::swap; swap(a,b); return 0; } causes both g++ and clang to issue the following compiler error on my system: $ clang -std=c++11 swap_repro.cpp -I. swap_repro.cpp:28:3: error: call to 'swap' is ambiguous swap(a,b); ^~~~ /usr/bin/../lib/gcc/x86_64-linux-gnu/5.2.1/

Nested template argument deduction for class templates not working

混江龙づ霸主 提交于 2019-12-03 06:29:41
In this Q&A I wrote a little wrapper class that provides reverse iterator access to a range, relying on the c++1z language feature template argument deduction for class templates ( p0091r3 , p0512r0 ) #include <iostream> #include <iterator> #include <vector> template<class Rng> class Reverse { Rng const& rng; public: Reverse(Rng const& r) noexcept : rng(r) {} auto begin() const noexcept { using std::end; return std::make_reverse_iterator(end(rng)); } auto end() const noexcept { using std::begin; return std::make_reverse_iterator(begin(rng)); } }; int main() { std::vector<int> my_stack; my

Alias templates used in SFINAE lead to a hard error

痴心易碎 提交于 2019-12-02 06:42:38
问题 I want to use an enabler (an alias template of enable_if ), defined in one class template, in another class template. It looks like this: template< ... > using enabler = typename std::enable_if< ... >::type; This works fine for SFINAE. But when I add another alias template in the second class like template< ... > using enabler = typename first_class<..> :: template enabler< ... >; and use this enabler for SFINAE, substitution (correctly) fails but with a hard error for g++4.8.0 and 4.8.1.

Avoid angle brackets in default template

假装没事ソ 提交于 2019-11-29 02:12:21
If I have a template class with a default template type, I have to write the template angle brackets. Is it somehow possible to avoid this? Example: template <typename T=int> class tt { public: T get() { return 5; } }; ... tt<> t; // how to avoid <> std::cout << t.get() << std::endl; Until now i've did this by a separate namespace and redeclaring the class: namespace detail_ { template <typename T=int> class tt { public: T get() { return 5; } }; } class tt : public detail_::tt {} ... tt t; std::cout << t.get() << std::endl; The problem is, if I want to use the class with an other type I have

Avoid angle brackets in default template

馋奶兔 提交于 2019-11-27 16:32:29
问题 If I have a template class with a default template type, I have to write the template angle brackets. Is it somehow possible to avoid this? Example: template <typename T=int> class tt { public: T get() { return 5; } }; ... tt<> t; // how to avoid <> std::cout << t.get() << std::endl; Until now i've did this by a separate namespace and redeclaring the class: namespace detail_ { template <typename T=int> class tt { public: T get() { return 5; } }; } class tt : public detail_::tt {} ... tt t;