partial-specialization

Partial class template specialization with maps

て烟熏妆下的殇ゞ 提交于 2019-12-24 16:02:17
问题 I'm a new C++ programmer, I learned Java and ANSI C a time ago and decided to give it a shot. Well, I love C++, but I didn't like how the iterators work: In java, you could make a whole container private and implement a getter function to it's iterator, and the iterator has a method hasNext() that returns a boolean depending on if it has reached the end of the container. The only way I found to do something similar on C++ is writing 2 getters, iteratorBegin() and iteratorEnd() , that returned

Class member function template partial specialization with variadic parameters [closed]

限于喜欢 提交于 2019-12-24 10:27:37
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . I'm using Visual Studio 2017 CE version 15.6.2 with compiler language options set to: ISO C++ Latest Draft Standard (/std:c++latest) I'm working with a majority of the functions from <random> and I have 2 classes that are non template classes, RandomEngine and RandomDistribution . These classes can not be

Understanding partial specialization of inherited nested class templates

谁都会走 提交于 2019-12-24 07:46:00
问题 This question is connected to a previous Q&A in which a bug report for gcc was mentioned (supposedly fixed in gcc 4.5.0) and concerns some peculiarities for partial specialization of nested class template. My setup is that I have a class Base with a nested class template Inner that is partially specialized for char (using the dummy parameter trick, because explicit speciliaztion is not allowed in-class). #include <type_traits> #include <iostream> #include <ios> struct Base { // dummy template

Partial specialization for pointers, c++

允我心安 提交于 2019-12-23 15:10:40
问题 How to make partial specialization of the class GList so that it is possible to store pointers of I (i.e I*) ? template <class I> struct TIList { typedef std::vector <I> Type; }; template <class I> class GList { private: typename TIList <I>::Type objects; }; 回答1: You don't need to specialise to allow that. It can store pointers already. GList<int*> ints; Anyway, if you wanted to specialise GList for pointers, use the following syntax. template <class I> class GList<I*> { ... }; Then just use

Static table generation works with GCC but not clang; is clang bugged?

大兔子大兔子 提交于 2019-12-23 09:59:46
问题 I wrote some code once upon a time that generated a static table/array at compile time for some template metaprogramming (the idea is that C-style strings can be built at compile time (they're just char arrays)). The idea and the code is based off David Lin's answer: #include <iostream> const int ARRAY_SIZE = 5; template <int N, int I=N-1> class Table : public Table<N, I-1> { public: static const int dummy; }; template <int N> class Table<N, 0> { public: static const int dummy; static int

C++ - Overload templated class method with a partial specilization of that method

狂风中的少年 提交于 2019-12-23 05:13:09
问题 There are a few questions already similar to this already on stack overflow, but nothing that seemd to directly answer the question I have. I do apologise if I am reposting. I'd like to overload a few methods of a templated class (with 2 template parameters) with a partial template specialisation of those methods. I haven't been able to figure out the correct syntax, and am starting to think that it's not possible. I thought I'd post here to see if I can get confirmation. Example code to

C++ template specialization via a base class

旧巷老猫 提交于 2019-12-23 03:02:02
问题 I want to be able to make the compiler shout when i call a constructor of foo with a class that is NOT derived from _base*. The current code allows only for foo<_base*> itself. Any easy solution ? class _base { public: // ... }; class _derived: public _base { public: // ... }; template <typename T> class foo { public: foo () { void TEMPLATE_ERROR; } }; template <> foo<_base*>::foo () { // this is the only constructor } main-code: foo<_base*> a; // should work foo<_derived*> b; // should work

C++ template specialization via a base class

我只是一个虾纸丫 提交于 2019-12-23 03:01:07
问题 I want to be able to make the compiler shout when i call a constructor of foo with a class that is NOT derived from _base*. The current code allows only for foo<_base*> itself. Any easy solution ? class _base { public: // ... }; class _derived: public _base { public: // ... }; template <typename T> class foo { public: foo () { void TEMPLATE_ERROR; } }; template <> foo<_base*>::foo () { // this is the only constructor } main-code: foo<_base*> a; // should work foo<_derived*> b; // should work

specializing functions on stl style container types

六眼飞鱼酱① 提交于 2019-12-22 10:54:02
问题 If i have a type T , what is a useful way to inspect it at compile time to see whether its an STL-style container (for an arbitrary value type) or not? (Assumption: pointers, reference, etc. already stripped) Starting code: template<class T> // (1) void f(T&) {} template<class T> // (2) void f(std::vector<T>&) {} void test() { int a; std::vector<int> b; f(a); f(b); } Now this works fine, but what if i want to generalize the container (i.e. not define (3) , (4) , ... explicitly)? Utilizing

Template specialisation with default argument [duplicate]

血红的双手。 提交于 2019-12-22 09:29:43
问题 This question already has answers here : How does `void_t` work (2 answers) Closed last year . I have a program that is as follows. There is a base template struct X and a partial specialisation with SFINAE. template <typename T, typename U = void> struct X{ X() { std::cout << "in 1" << std::endl; }; }; template <typename T> struct X< T, std::enable_if_t<std::is_integral_v<T>> > { X() { std::cout << "in 2" << std::endl; }; }; int main() { X<int> x; } When running the program in 2 is printed.