Topic: [Q] Redefinition of default argument


Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/05/31
Raw View
Hi,

Can anybody explain why the results of two compilations (see below)
are different?
Why Program#2 is incorrect?

        Thanks in advance,
        Alex


//=================== Program#1 ===========================
//------------------- C++ code : BEGIN --------------------

template <typename T1, unsigned int T2>
class AAA {};

template <typename T1>
class BBB {template <unsigned int T2> friend class AAA<T1, T2>;};

int main()
{
BBB<int>        b1;
        return 0;
}


//------------------- C++ code : END ----------------------

//------------------- Compilation Results : BEGIN ---------

OK

//------------------- Compilation Results : END -----------





//=================== Program#2 ===========================
//------------------- C++ code : BEGIN --------------------

template <typename T1, unsigned int T2 = 2>     // Line#1
class AAA {};

template <typename T1>
class BBB {template <unsigned int T2> friend class AAA<T1, T2>;};

int main()
{
BBB<int>        b1;                             // Line#9
        return 0;
}


//------------------- C++ code : END ----------------------

//------------------- Compilation Results : BEGIN ---------

main.C: In instantiation of `BBB<int>':
main.C:9:   instantiated from here
main.C:9: redefinition of default argument for `unsigned int const T2'
main.C:1:   original definition appeared here

//------------------- Compilation Results : END -----------





//#########################################################
//------------------- Compiler & System  ------------------

g++ -v     : gcc version egcs-2.91.57 19980901
             (egcs-1.1 release)

uname -a   : SunOS <nodename> 5.6 Generic_105181-09
             sun4m sparc SUNW,SPARCstation-5

//---------------------------------------------------------

//#########################################################






[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: wmm@fastdial.net
Date: 1999/06/02
Raw View
In article <37528047.506D0413@tibam.elex.co.il>,
  Alex Vinokur <alexander.vinokur@telrad.co.il> wrote:
> Why Program#2 is incorrect?
>
> template <typename T1, unsigned int T2 = 2>     // Line#1
> class AAA {};
>
> template <typename T1>
> class BBB {template <unsigned int T2> friend class AAA<T1, T2>;};

The problem is that the friend declaration is a partial
specialization, and that's a no-no according to 14.5.3p9.  You
either have to declare _all_ specializations of AAA to be
friends or one particular specialization to be a friend; you
can't make only _some_ specializations friends.  I think the
best you can do here is

 template <typename T1>
 class BBB {
     template <typename, unsigned> friend class AAA;
 };

which will make all specializations of AAA to be friends of BBB.

(Yes, that was a problem with the first program, too; I don't
know why you got the particular errors or lack thereof you did.)

--
William M. Miller, wmm@fastdial.net
Software Emancipation Technology (www.setech.com)


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]