By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,503 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,503 IT Pros & Developers. It's quick & easy.

Function specialization

P: n/a
Hi,
today a guy came to #C++@IRCNet and during discusion he showed this piece of
code:

template<bool tswitch=false>

class clstmp

{

public:

int val;
inline clstmp()

{

ctor_clstmp<tswitch>();

}


template<bool> inline void ctor_clstmp();

template<> inline void ctor_clstmp<false>()

{

val = 1;

}

template<> inline void ctor_clstmp<true>()

{

val = 2;

}

};

It does compile with VS.NET 7.1 compiler even with MS extensions disabled.
It doesn't compile with recent GCC 3.5 fresh from CVS and neither with
Comeau's web test drive compiler. Now I would like to hear ruling on whether
it is valid C++ code or not.

V.H.


Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Vaclav Haisman" <V.*******@sh.cvut.cz> wrote in message
news:cf***********@ns.felk.cvut.cz
Hi,
today a guy came to #C++@IRCNet and during discusion he showed this
piece of code:

template<bool tswitch=false>

class clstmp

{

public:

int val;
inline clstmp()

{

ctor_clstmp<tswitch>();

}


template<bool> inline void ctor_clstmp();

template<> inline void ctor_clstmp<false>()

{

val = 1;

}

template<> inline void ctor_clstmp<true>()

{

val = 2;

}

};

It does compile with VS.NET 7.1 compiler even with MS extensions
disabled. It doesn't compile with recent GCC 3.5 fresh from CVS and
neither with Comeau's web test drive compiler. Now I would like to
hear ruling on whether it is valid C++ code or not.

V.H.

According to Vandevoorde and Josuttis (C++ Templates: The Complete Guide, p.
199), you can only specialise member templates if the enclosing class is
also fully specialised. Your sample code does not fully specialise the
enclosing class, so is not legal.

The following is an example of legal code (Comeau compiles it):

template<bool tswitch=false>
class clstmp
{
public:
int val;
inline clstmp()
{
ctor_clstmp<tswitch>();
}

template<bool>
inline void ctor_clstmp();
};

template<>
template<>
inline void clstmp<true>::ctor_clstmp<false>()
{
val = 1;
}

template<>
template<>
inline void clstmp<true>::ctor_clstmp<true>()
{
val = 2;
}
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #2

P: n/a
John Carson wrote in news:41******@usenet.per.paradox.net.au in
comp.lang.c++:

According to Vandevoorde and Josuttis (C++ Templates: The Complete
Guide, p. 199), you can only specialise member templates if the
enclosing class is also fully specialised. Your sample code does not
fully specialise the enclosing class, so is not legal.


Does anybody know where this is stated in the *Standard* ?

I just spent over an hour looking, and didn't find it.

TIA.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

P: n/a

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
John Carson wrote in news:41******@usenet.per.paradox.net.au in
comp.lang.c++:

According to Vandevoorde and Josuttis (C++ Templates: The Complete
Guide, p. 199), you can only specialise member templates if the
enclosing class is also fully specialised. Your sample code does not
fully specialise the enclosing class, so is not legal.


Does anybody know where this is stated in the *Standard* ?

I just spent over an hour looking, and didn't find it.


14.7.3/2 "An explicit specialization shall be declared in the namespace of
which the template is a member, or, for member templates, in the namespace
of which the enclosing class or enclosing class template is a member. "

-Sharad
Jul 22 '05 #4

P: n/a
Sharad Kala wrote in news:2o************@uni-berlin.de in comp.lang.c++:

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
John Carson wrote in news:41******@usenet.per.paradox.net.au in
comp.lang.c++:
>
> According to Vandevoorde and Josuttis (C++ Templates: The Complete
> Guide, p. 199), you can only specialise member templates if the
> enclosing class is also fully specialised. Your sample code does
> not fully specialise the enclosing class, so is not legal.
>
>


Does anybody know where this is stated in the *Standard* ?

I just spent over an hour looking, and didn't find it.


14.7.3/2 "An explicit specialization shall be declared in the
namespace of which the template is a member, or, for member templates,
in the namespace of which the enclosing class or enclosing class
template is a member. "


Thanks, for some reason I got 14.7 and read "14.7 Template instantiation"
and missed the "... and specialization" :).

But 14.7.3/2 is only part of the OP's problem, the quote above from
"C++ Templates: The Complete Guide" also states that the *enclosing*
class template is fully specialized.

However you inspired me to keep reading and that bit is in 14.7.3/18.

Thanks again.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #5

P: n/a
"Vaclav Haisman" <V.*******@sh.cvut.cz> wrote:
[code reformatted] template<bool tswitch=false>
class clstmp
{
public:
int val;

inline clstmp() {
ctor_clstmp<tswitch>();
Why doesn't it complain that "ctor_clstmp" is an unrecognized symbol?
or is it OK as long as it has been declared at the point where an
object of this class type is created?

In fact my compiler allows:
inline clstmp() {
fucq ctor_clstmp<tswitch>();
jlk132j4l1k-11-;sa;f{~~~~}%%*)(*&
}
}

template<bool> inline void ctor_clstmp();

template<> inline void ctor_clstmp<false>() {
val = 1;
}
template<> inline void ctor_clstmp<true>() {
val = 2;
}
};


I get the delightful error message:
E2490 Specialization within template classes not yet implemented
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.