partial-specialization

A workaround for partial specialization of function template?

烂漫一生 提交于 2019-11-29 18:01:30
Consider the following metafunction for an integral pow (it is just an example) : class Meta { template<int N, typename T> static constexpr T ipow(T x) { return (N > 0) ? (x*ipow<N-1>(x)) : ((N < 0) ? (static_cast<T>(1)/ipow<N>(x)) : (1)) } }; How to write the stop condition for such a function ? Anytime you ask yourself "how to simulate partial specialization for functions", you can think "overload, and let partial ordering decide what overload is more specialized". template<int N> using int_ = std::integral_constant<int, N>; class Meta { template<int N, typename T> static constexpr T ipow(T

Template partial specialization with multiple template argument error

自古美人都是妖i 提交于 2019-11-29 09:52:02
问题 When I use template partial specialization on a class with one template argument, I can specialize a method like this: #include <cstdlib> template< std::size_t Dim > class Test { public: int foo(); }; template< std::size_t Dim > inline int Test< Dim >::foo() { return 0; } template<> inline int Test< 1 >::foo() { return 1; } int main() { Test< 2 > wTest2; Test< 1 > wTest1; wTest2.foo(); wTest1.foo(); return 0; } The method foo is specialized for Dim = 1. But as soon as I add a template

specialize a member template without specializing its parent

谁说我不能喝 提交于 2019-11-29 06:22:27
I have a class template nested inside another template. Partially specializing it is easy: I just declare another template< … > block inside its parent. However, I need another partial specialization that happens to specify all its local template arguments. This makes it into an explicit specialization. Explicit specializations, for whatever reason, must be at namespace scope. To declare it outside its parent class, the parent must be nominated, which requires a non-empty template argument list. This implies partial specialization. Partial specialization is what I'm doing, and it's supposed to

How to specialize only some members of a template class?

眉间皱痕 提交于 2019-11-29 02:56:27
问题 Code: template<class T> struct A { void f1() {}; void f2() {}; }; template<> struct A<int> { void f2() {}; }; int main() { A<int> data; data.f1(); data.f2(); }; ERROR: test.cpp: In function 'int main()': test.cpp:16: error: 'struct A<int>' has no member named 'f1' Basically, I only want to specialize one function and use the common definition for other functions. (In actual code, I have many functions which I don't want to specialize). How to do this? Thanks! 回答1: Consider moving common parts

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

守給你的承諾、 提交于 2019-11-28 17:59:16
This question already has an answer here: What is the meaning of “… …” token? i.e. double ellipsis operator on parameter pack 2 answers 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 line 444 : template<typename _Res, typename... _ArgTypes> struct is_function<_Res(_ArgTypes......) volatile &&> : public

Template parameters not used in partial specialization

坚强是说给别人听的谎言 提交于 2019-11-28 13:55:15
I have the following code: template<typename T, typename Allocator = std::allocator<T> > class Carray { // ... typedef T* pointer; typedef pointer iterator; // ... }; Now I'm trying to do partial specialization for iterator_traits . It seems OK to me, but g++ 4.4.5 complains: #include <iterator> namespace std { template<typename T, typename Allocator> struct iterator_traits<typename Carray<T, Allocator>::iterator> { // line 128 typedef T value_type; typedef typename Allocator::difference_type difference_type; typedef typename Allocator::reference reference; typedef typename Allocator::pointer

Tag dispatch versus static methods on partially specialised classes

一个人想着一个人 提交于 2019-11-28 02:53:59
Suppose I want to write a generic function void f<T>() , which does one thing if T is a POD type and another thing if T is non-POD (or any other arbitrary predicate). One way to achieve this would be to use a tag-dispatch pattern like the standard library does with iterator categories: template <bool> struct podness {}; typedef podness<true> pod_tag; typedef podness<false> non_pod_tag; template <typename T> void f2(T, pod_tag) { /* POD */ } template <typename T> void f2(T, non_pod_tag) { /* non-POD */ } template <typename T> void f(T x) { // Dispatch to f2 based on tag. f2(x, podness<std::is

specialize a member template without specializing its parent

南笙酒味 提交于 2019-11-27 23:53:02
问题 I have a class template nested inside another template. Partially specializing it is easy: I just declare another template< … > block inside its parent. However, I need another partial specialization that happens to specify all its local template arguments. This makes it into an explicit specialization. Explicit specializations, for whatever reason, must be at namespace scope. To declare it outside its parent class, the parent must be nominated, which requires a non-empty template argument

partial specialization ordering with non-deduced context

泪湿孤枕 提交于 2019-11-27 22:26:39
According to [temp.class.order] §14.5.5.2, the selection of a partial specialization of t in this example: template< typename > struct s { typedef void v, w; }; template< typename, typename = void > struct t {}; template< typename c > struct t< c, typename c::v > {}; template< typename c > struct t< s< c >, typename s< c >::w > {}; t< s< int > > q; is equivalent to the selection of an overload of f in this example: template< typename > struct s { typedef void v, w; }; template< typename, typename = void > struct t {}; template< typename c > constexpr int f( t< c, typename c::v > ) { return 1;

Why is it disallowed for partial specialization in a non-type argument to use nested template parameters

本小妞迷上赌 提交于 2019-11-27 20:01:24
I have this code template<int N, bool C = true> struct A; template<int N> struct A<N, !(N % 5)> { /* ... */ }; // should work A<25> a; That is, for numbers N that are divisible by 5 , the compiler should use the partial specialization. But the compiler won't accept that partial specialization, because the Standard requires it to reject such code where a non-type argument of a partial specialization references a parameter and is not simply a parameter (like, A<N, N> would be valid). But what is the reason of doing so? Note that I can simply change my code to a more wordy example and it is valid