[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::c_plus_plus

Title:C++
Notice:Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS)
Moderator:DECCXX::AMARTIN
Created:Fri Nov 06 1987
Last Modified:Thu Jun 05 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3604
Total number of notes:18242

3521.0. "Constant expression templates and friend classes." by PEACHS::LAMPERT (Pat Lampert, UNIX Applications Support, 343-1050) Mon Mar 31 1997 16:41

I'm having trouble defining a friend function of a template class whose
parameter is a constant expression.  I looked through the ARM and I'm still
not any wiser.

**************************** Compiler output
cxx -c templfr.cpp
cxx: Error: templfr.cpp, line 13: The function template "__ls" has a non-type parameter "size".
template <int size> 
----------^
**************************** Code Example
// templr.h

template <int size>
class C {
public:
   C(const char* ch_);

friend ostream& 
operator<<(ostream& os);


private:
   char ch[size+1];
};

-------------------------- 8< ---------------------/
// templfr.cpp
#include "templfr.h"
#include <string.h>

template <int size>
C<size>::C(const char* ch_)
{
   ::strncpy( &ch[0], &ch_[0],
              (::strlen(ch_) > (size))? (size) : ::strlen(ch_));
}

template <int size> 
ostream& operator<< (ostream& os, const C& c)
// How do I define this function ?
{
    return os << &c.ch[0] << endl;
}

T.RTitleUserPersonal
Name
DateLines
3521.1Same problem as in note 3397?PEACHS::LAMPERTPat Lampert, UNIX Applications Support, 343-1050Mon Mar 31 1997 19:1812
Just realized thia may already be documented 
as a restriction. Am I correct?

--------------------------------------------------------------------------------
>As documented in Section 1.4.7 of the V5.5 release notes,
>under "Current Restrictions":
>
>             o  The compiler does not accept a parameter-declaration
>                 (non-type template parameters) for function templates.


Pat
3521.2DECCXX::COLEENWed Apr 02 1997 16:2913

Yes, you have found a restriction with the current release of
DEC C++.  We have lifted this restriction in the next major
release of the compiler - V6.0 which is coming out later this
year.

The only workaround I can think of is to make the << operator
a member function rather than a friend, but that may not be
what you want it to be.

Thanks,
Coleen