Private template specialization

Does anyone know according to what rules the code does not compile?

template <class T>
struct B
{
    typedef T type;
};

template<class T>
struct X
{
};
template<class T>
struct X<B<T>::type*>//HERE I'M PARTIALLY SPECIALIZING (WELL, TRYING TO...)
{
};

Please see the comment inside the code.

+3
source share
3 answers

What do you think will work? The compiler will look if there is a class T somewhere that has a typedef type of type for your class?

It just won't be. Although this is a pointer.

Remember that presumably your pattern B seems to be specialized in places, so the type is not always T *, but it cannot output it using reverse engineering.

For those who do not fully understand my answer, what you are asking the compiler to do is find the class U, so B :: type is the class that you pass as a parameter.

class Foo;
class Bar;

template<> struct B<Foo>
{
  typedef int type;
};

template<> struct B<Bar>
{
  typedef int type;
};

X<int*> // ambiguous, T is Foo or Bar?

, , . , , .

+9

typename ,

template<class T>
struct X<typename B<T>::type*>
{
};

, B<T>::type . typename !

-

EDIT:

typename . , , T B<T> X<U> , , . , .

. :


, :

template<class T>
struct X<B<T> >
{
};

, , .

+4

, typename, Nawaz.

, : " B<T>::type*. , B<T>::type T T. :

class MyClass1 {};
typedef typename B<MyClass>::type MyClass2; //(*)

X<MyClass*> obj1;
X<MyClass2*> obj2;

(*) MyClass2, MyClass1. , obj1 obj2 . , X ?

X, , , (*) (, , obj2). obj1 X, (*) .

, , - B<T>::type, . , , , typedef .

, , ​​.


,

I believe that your problem can be attacked by creating a class of attributes for explicitly marking types that must be handled in a special way. Something like that:

template <bool v>
struct boolean_value {
  static const bool value=v;
};

template <typename T>
struct is_my_interesting_type : public boolean_value<false> {};

class MyClass {
  ...
};

template <>
struct is_my_interesting_type<MyClass> : public boolean_value<true> {};

template <typename T, bool special>
class  InternalX {
  ... //generic version of your template X
};

template <typename T>
class InternalX<T,true> {
  ... //special version of your template X
};

template <typename T>
class X : public InternalX<T,is_my_interesting_type<T>::value> {};

In addition, you may be wondering how this is done in the boost library, in particular Boost.Type_Traits

0
source

Source: https://habr.com/ru/post/1796645/


All Articles