Templates as a templates-parameters 
Author Message
 Templates as a templates-parameters

I needs to compile such sample code in Visual C++:

template <class _FLT> class cPoint
{
public:
      _FLT  x, y;

      cPoint () : x( 0 ), y( 0 ) {}

Quote:
};

template <class _FLT, template <class T> class point_type = cPoint>
class cRect
{
public:
      point_type< _FLT > t_l;
      point_type< _FLT > b_r;

      cRect () : t_l(), b_r() {}

      void  reset ();

Quote:
};

template <class _FLT, template <class T> class point_type>
void cRect< _FLT, point_type >::reset ()
{
      t_l.x = t_l.y = b_r.x = b_r.y = 0;

Quote:
}

int   main ()
{
      cRect<double> rect;

      rect.t_l.x = 0;
      rect.t_l.y = 0;

      rect.b_r.x = 10;
      rect.b_r.y = 10;

      rect.reset();

      return 0;

Quote:
}

But VC++ reports an error a-la "templates definitions can't nest"!

Can You explain what is wrong or what settings should i set to avoid
this error?
Thanks in advance!



Sun, 02 Feb 2003 03:00:00 GMT  
 Templates as a templates-parameters



Quote:
> I needs to compile such sample code in Visual C++:
> template <class _FLT> class cPoint

Identifiers with leading underscore and uppercase letter are reserved for
the implementation. See 17.4.3.1.2 in the standard.

Quote:
>{
> public:
>      _FLT  x, y;

>      cPoint () : x( 0 ), y( 0 ) {}
>};
>template <class _FLT, template <class T> class point_type = cPoint> class
cRect
>{
>public:
>      point_type< _FLT > t_l;
>      point_type< _FLT > b_r;

>      cRect () : t_l(), b_r() {}

>      void  reset ();
>};
>[snip]
>But VC++ reports an error a-la "templates definitions can't nest"!
>Can You explain what is wrong or what settings should i set to avoid this
error?
>Thanks in advance!

Template template parameters are not supported in VC6.
Instead you can try traits approach:

template <class NumType> class cPoint
{
public:
  NumType  x, y;
  cPoint () : x( 0 ), y( 0 ) {}

Quote:
};

template<class NumType>
struct point_traits {
  typedef cPoint<NumType> type;

Quote:
};

template<class NumType, class PtTr = point_traits<NumType>/**/>
class cRect
{
public:
  typedef typename PtTr::type point_type;
  point_type t_l;
  point_type b_r;
  /*rest of the class*/

Quote:
};

Todor.


Sun, 02 Feb 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Template class as template parameter

2. Template class as template parameter

3. VC7 does NOT support template-template parameters involving non-type parameters

4. VC 7.1 final beta: bug with template template parameters

5. template template parameter with default value (VS.NET 2003)

6. Possible bug with template template parameters in VC++ 7.0

7. Bug in inheritance from template template parameters in VC7

8. template template parameters

9. template with template parameter

10. template template parameter that VC++ does NOT support

11. Template Template Parameters

12. template template parameter and static var definition

 

 
Powered by phpBB® Forum Software