template-specialization

Why does the Standard prohibit friend declarations of partial specializations?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 10:20:00
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 back to the C++98 standard, so presumably the rationale is quite basic and clear. Can someone explain it

Multiple SFINAE class template specialisations using void_t

大城市里の小女人 提交于 2019-12-03 09:42:12
Are multiple class template specialisations valid, when each is distinct only between patterns involving template parameters in non-deduced contexts? A common example of std::void_t uses it to define a trait which reveals whether a type has a member typedef called "type". Here, a single specialisation is employed. This could be extended to identify say whether a type has either a member typedef called "type1", or one called "type2". The C++1z code below compiles with GCC, but not Clang. Is it legal? template <class, class = std::void_t<>> struct has_members : std::false_type {}; template

C++ template specialization of function: “illegal use of explicit template arguments”

谁都会走 提交于 2019-12-03 08:36:02
问题 The following template specialization code: template<typename T1, typename T2> void spec1() { } Test case 1: template< typename T1> //compile error void spec1<int>() { } Test case 2: template< typename T2> //compile error void spec1<int>() { } generates the following compilation error: error C2768: 'spec1' : illegal use of explicit template arguments Does anyone know why? 回答1: Function templates cannot be partially specialised, only fully, i.e. like that: template<> void spec1<char, int>() {

Is it possible to specialize a template definition based on the existence of a nested typedef of a template type parameter?

。_饼干妹妹 提交于 2019-12-03 08:03:58
I have a template, template <typename T> class wrapper , that I would like to specialize based on the existence of typename T::context_type . If typename T::context_type is declared, then the constructors and assignment operator overloads of the wrapper<T> instantiation should accept a mandatory typename T::context_type parameter. Additionally, wrapper<T> objects would store "context" in the member data. If typename T::context_type does not exist, then the constructors and assignment operator overloads of wrapper<T> would take one less parameter and there would be no additional data member. Is

Circumventing template specialization

老子叫甜甜 提交于 2019-12-03 06:30:24
问题 Suppose I am a user of a Certain Template Library ( CTL ) which defines a template, named, say, Hector template <class T> class Hector {...}; And in its documentation it gives many guarantees about Hector template behavior. But then it also defines a specialization for a certain type Cool template <> class Hector<Cool> {....}; The purpose of the specialization is a more optimized implementation of Hector , but unfortunately because of this optimization many guarantees of Hector are violated.

Good practices regarding template specialization and inheritance

北慕城南 提交于 2019-12-03 04:15:43
问题 Template specialization does not take into account inheritance hierarchy. For example, if I specialize a template for Base and instantiate it with Derived , the specialization will not be chosen (see code (1) below). This can be a major hindrance, because it sometimes lead to violation of the Liskov substitution principle. For instance, while working on this question, I noticed that I could not use Boost.Range algorithms with std::sub_match while I could with std::pair . Since sub_match

c++ function template specialization for known size typedefed array

╄→尐↘猪︶ㄣ 提交于 2019-12-03 04:02:36
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void func( Type var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; std::cout << "-> var is SCALAR. Size = " << sizeof( Type ) << std::endl; } #if 1 template< typename Type > void func( Type * var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; std::cout << "-> var is ARRAY. Size = " << sizeof( Type * ) << std::endl; } #endif int main( ) { typedef char char16[ 16 ]; char16 c16 =

C++ template specialization of function: “illegal use of explicit template arguments”

﹥>﹥吖頭↗ 提交于 2019-12-03 00:10:01
The following template specialization code: template<typename T1, typename T2> void spec1() { } Test case 1: template< typename T1> //compile error void spec1<int>() { } Test case 2: template< typename T2> //compile error void spec1<int>() { } generates the following compilation error: error C2768: 'spec1' : illegal use of explicit template arguments Does anyone know why? Function templates cannot be partially specialised, only fully, i.e. like that: template<> void spec1<char, int>() { } For why function templates cannot be partially specialised, you may want to read this . When you

Circumventing template specialization

安稳与你 提交于 2019-12-02 20:05:44
Suppose I am a user of a Certain Template Library ( CTL ) which defines a template, named, say, Hector template <class T> class Hector {...}; And in its documentation it gives many guarantees about Hector template behavior. But then it also defines a specialization for a certain type Cool template <> class Hector<Cool> {....}; The purpose of the specialization is a more optimized implementation of Hector , but unfortunately because of this optimization many guarantees of Hector are violated. Currently I really don't need the optimization, I'd rather preserve all the guarantees of Hector . Is

How to enforce template parameter class to derive from super with an anonymous template parameter

徘徊边缘 提交于 2019-12-02 19:11:56
问题 I have a couple of template classes template < class Cost > class Transition { public: virtual Cost getCost() = 0; }; template < class TransitionCl, class Cost > class State { protected: State(){ static_assert( std::is_base_of< Transition< Cost >, TransitionCl >::value, "TransitionCl class in State must be derived from Transition< Cost >" ); } public: virtual void apply( const TransitionCl& ) = 0; }; and I would rather not have to pass Cost into State , because State is completely independent