Function specialization

V

Vaclav Haisman

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.
 
J

John Carson

Vaclav Haisman said:
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;
}
 
R

Rob Williscroft

John Carson wrote in 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.
 
S

Sharad Kala

Rob Williscroft said:
John Carson wrote in in
comp.lang.c++:


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
 
R

Rob Williscroft

Sharad Kala wrote in in comp.lang.c++:
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.
 
O

Old Wolf

Vaclav Haisman said:
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top