partial-specialization

Why can't you partially specialize a class member function?

て烟熏妆下的殇ゞ 提交于 2021-01-29 05:03:36
问题 Member functions of template classes can be fully specialized, e.g. template<class A> struct MyClass { // Lots of other members int foo(); }; template<class A> MyClass<A>::foo() { return 42; } template<> MyClass<int>::foo() { return 0; } would compile without problems. Note that foo() is not a template function so this is not about template function specialization (I can understand partial specialization is not allowed there as it would become incredibly confusing in combination with

Partial template specialization with mismatching `int` and `size_t` not compiling

為{幸葍}努か 提交于 2020-03-21 11:33:57
问题 With reference to the following code #include <utility> #include <cassert> template <typename T> struct Wot; template <int... ints> struct Wot<std::index_sequence<ints...>> {}; int main() { assert(sizeof(Wot<std::index_sequence<1, 2, 3>>) == 1); } This works on clang but does not work on gcc, when I change the type of the partial specialization to accept std::size_t in the index sequence however it works. Who is right? Clang or gcc? See this in action here https://wandbox.org/permlink

Partial template specialization with mismatching `int` and `size_t` not compiling

百般思念 提交于 2020-03-21 11:33:06
问题 With reference to the following code #include <utility> #include <cassert> template <typename T> struct Wot; template <int... ints> struct Wot<std::index_sequence<ints...>> {}; int main() { assert(sizeof(Wot<std::index_sequence<1, 2, 3>>) == 1); } This works on clang but does not work on gcc, when I change the type of the partial specialization to accept std::size_t in the index sequence however it works. Who is right? Clang or gcc? See this in action here https://wandbox.org/permlink

Better pattern for partial specialization disambiguation precedence chain?

拈花ヽ惹草 提交于 2020-01-13 11:39:28
问题 Consider the following series of partial specializations: template <typename T, typename Enable=void> struct foo { void operator()() const { cout << "unspecialized" << endl; } }; template <typename T> struct foo<T, enable_if_t< is_integral<T>::value >>{ void operator()() const { cout << "is_integral" << endl; } }; template <typename T> struct foo<T, enable_if_t< sizeof(T) == 4 and not is_integral<T>::value >>{ void operator()() const { cout << "size 4" << endl; } }; template <typename T>

What is wrong with partial template specialization?

别等时光非礼了梦想. 提交于 2020-01-02 07:06:23
问题 I am writing a templated class with one type paramenter and one boolean, here is the code: template<class T, bool p = true> class A { private: T* ptr; public: A(); }; template<class T> A<T,true>::A() { ptr = 0xbaadf00d; } int main() { A<int> obj; A<int, false> o; return(0); } And I am getting these compilation errors: Error 1 error C3860: template argument list following class template name must list parameters in the order used in template parameter list tst.cpp 15 Error 2 error C2976: 'A<T

Partial specialisation of member function with non-type parameter

泄露秘密 提交于 2020-01-01 08:43:21
问题 I have a template class with both a type and a non-type template parameter. I want to specialize a member function, what I finding is, as in the example below, I can do a full specialization fine. template<typename T, int R> struct foo { foo(const T& v) : value_(v) {} void bar() { std::cout << "Generic" << std::endl; for (int i = 0; i < R; ++i) std::cout << value_ << std::endl; } T value_; }; template<> void foo<float, 3>::bar() { std::cout << "Float" << std::endl; for (int i = 0; i < 3; ++i)

C++ template partial specialization - specializing one member function only

十年热恋 提交于 2019-12-30 00:36:09
问题 Bumped into another templates problem: The problem: I want to partially specialize a container-class (foo) for the case that the objects are pointers, and i want to specialize only the delete-method. Should look like this: The lib code template <typename T> class foo { public: void addSome (T o) { printf ("adding that object..."); } void deleteSome (T o) { printf ("deleting that object..."); } }; template <typename T> class foo <T *> { public: void deleteSome (T* o) { printf ("deleting that

What are the 6 dots in template parameter packs? [duplicate]

 ̄綄美尐妖づ 提交于 2019-12-29 03:15:06
问题 This question already has answers here : What is the meaning of “… …” token? i.e. double ellipsis operator on parameter pack (2 answers) Closed 5 years ago . While looking at this question I found myself in the cpp reference site where I noticed a strange and new to me syntax : template<class Ret, class... Args> struct is_function<Ret(Args......)volatile &&> : std::true_type {}; Yep, 6 dots ! Initially I thought this was a typo, but after checking the libstdc++ source again there it was eg at

Specializing and or Overloading member function templates with variadic parameters

淺唱寂寞╮ 提交于 2019-12-25 01:48:54
问题 Trying to resolve overload resolution for class member: static function template overload - partial specialization. I currently have a class declared / defined as such: Note: my use of Param a , Param b , Param c etc. are not related to the actual declarations / definitions directly. These can be any arbitrary type that is passed into the functions for example: it could be int a , enum b , char c . I'm just using this to only show the pattern of the declarations, however all of the different

Partial class template specialization with maps

为君一笑 提交于 2019-12-24 16:12:32
问题 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