template-specialization

C++ Templates: Partial Template Specifications and Friend Classes

别来无恙 提交于 2019-12-05 03:34:51
is it possible to somehow make a partial template specification a friend class? I.e. consider you have the following template class template <class T> class X{ T t; }; Now you have partial specializations, for example, for pointers template <class T> class X<T*>{ T* t; }; What I want to accomplish is that every possible X<T*> is a friend class of X<S> for ANY S . I.e. X<A*> should be a friend of X<B> . Of course, I thought about a usual template friend declaration in X: template <class T> class X{ template <class S> friend class X<S*>; } However, this does not compile, g++ tells me this: test4

Type_traits *_v variable template utility order fails to compile

帅比萌擦擦* 提交于 2019-12-05 03:26:34
Having seen this answer , I tried to come up with a variable template utility to the code from it: template <class T, template <class...> class Template> struct is_specialization : std::false_type {}; template <template <class...> class Template, class... Args> struct is_specialization<Template<Args...>, Template> : std::true_type {}; And implement it like so: template <template <class...> class Template, class... Args> constexpr bool is_specialization_v = is_specialization<Template<Args...>, Template>::value; because that's how I saw similar utilities to be implemented in the header file of

How to extract the highest-indexed specialization from a structure?

亡梦爱人 提交于 2019-12-05 01:33:39
I'm trying to do some template metaprogramming and I'm finding the need to "extract" the highest index of a specialization of some structure in some type. For example, if I have some types: struct A { template<unsigned int> struct D; template<> struct D<0> { }; }; struct B { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; }; struct C { template<unsigned int> struct D; template<> struct D<0> { }; template<> struct D<1> { }; template<> struct D<2> { }; }; How can I then write a metafunction like this: template<class T> struct highest_index { typedef ???

C++ template specialization on functions

北城以北 提交于 2019-12-05 00:56:04
I'm playing around with template specialization, and I've found an issue I can't seem to solve; this is my code: template<int length, typename T> void test(T* array) { ... test<length-1>(array); } template<typename T> void test<0>(T* array) { return; } So what I'm trying to do, is to pass the length, of what's to be processed in the template. The problem is, that the compilation of this, well outputs forever: a.cpp:83:43: error: template-id 'test<0>' in declaration of primary template a.cpp: In function 'void test(T*) [with int length= -0x000000081, T = int]': a.cpp:77:9: instantiated from

Getting “illegal use of explicit template arguments” when doing a pointer partial specialization for a class method

可紊 提交于 2019-12-04 23:45:20
Hello I'm having problems with partial specialization. What I want to do is have a class that has a template member function that will interpret a given value to one specified by the user. For instance the class name is Value and here is a snippet of what I want to do: int *ptr1 = new int; *ptr1 = 10; Value val1 = ptr1; int *ptr2 = val1.getValue<int*>(); Value val2 = 1; int testVal = val2.getValue<int>(); Here is how I implemented such class: struct Value { Value(void *p) : val1(p){} Value(int i) : val2(i){} template<typename T> T getValue(); void *val1; int val2; }; template<typename T> T*

SFINAE template specialization precedence

半腔热情 提交于 2019-12-04 23:04:10
#include <iostream> #include <array> #include <vector> template <typename T, typename SFINAE=void> struct trait; template <typename T> struct trait<T, decltype( std::declval<const T&>().begin(), std::declval<const T&>().end(), void() )> { static const char* name() { return "Container"; } }; template <typename T, std::size_t N> struct trait<std::array<T,N>> { static const char* name() { return "std::array"; } }; int main(int argc, char* argv[]) { std::cout << trait<std::vector<int>>::name() << std::endl; std::cout << trait<std::array<int,2>>::name() << std::endl; } I was expecting the third

How to create specialization for a single method in a templated class in C++?

穿精又带淫゛_ 提交于 2019-12-04 19:13:18
Many questions have been asked and they are similar to the one I am going to ask here, but they are not the same I think. I have a templated class: namespace app { template <typename T> class MyCLass { public: void dosome(); void doother(); } } /*ns*/ And implementations: template <typename T> app::MyClass<T>::dosome() {} template <typename T> app::MyClass<T>::doother() {} When I have an instance of that class to which a char is provided as template parameter, I want function dosome() to behave in a totally different way. But I just want that function to behave differently, everything else

Specialize Many Templates for a Set of Types

旧城冷巷雨未停 提交于 2019-12-04 18:22:34
How to specialize many template for all kinds of scalar values? (such as int , float , size_t , uint32_t , and types defined in the stdint header)? Can I avoid specializing each template for each of the types? I don't want to use boost or other non-standard libraries if possible. There are some solutions at template specialization for a set of types : Replace each template with multiple functions. One function for each scalar type. (But there are many templates. That would mean writing many functions.) Fail if the template takes a non-scalar type. (But I also want to write template for array

Is it possible to implement always_false in the C++ standard library?

痞子三分冷 提交于 2019-12-04 16:30:13
问题 There are cases where one uses an always_false helper to e.g. cause unconditional static_assert failure if instantiation of some template is attempted: template <class... T> struct always_false : std::false_type {}; template<class T> struct UsingThisShouldBeAnError { static_assert(always_false<T>::value, "You should not use this!"); }; This helper is necessary because a template definition must (at least theoretically) have at least one set of template parameters for which a valid

Why does the Standard prohibit friend declarations of partial specializations?

断了今生、忘了曾经 提交于 2019-12-04 16:24:56
问题 The C++ standard prohibits friend declarations of partial specializations. (§14.5.3/8): Friend declarations shall not declare partial specializations. [Example: template<class T> class A { }; class X { template <class T> friend class A<T*>; //error }; --end example] Other questions, e.g. this one, have received answers that invoke this prohibition, but I would like to know the rationale. I don't see it and can't find it with my favourite search engine. I can find however that it goes right