473,480 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Explicit specialization of member class template

The following code compiles with VC8
but fails to compiles with Comeau online,

[Error
"ComeauTest.c", line 7: error: explicit specialization is not allowed in the
current scope
template <>
^

"ComeauTest.c", line 10: error: explicit specialization is not allowed
in the
current scope
template <>
^
End-Error]
I locate the standard here:

[STD
14.7.3 Explicit specialization [temp.expl.spec]
An explicit specialization of any of the following:
¡ª function template
¡ª class template
¡ª member function of a class template
¡ª static data member of a class template
¡ª member class of a class template
¡ª member class template of a class template
¡ª member function template of a class template
can be declared by a declaration introduced by template<>

End-STD]

if I didn't get it wrong, we can have member class template explicit
specialization if the outter class is not templated. Right?
///////////////////////////////////////////

struct A
{
private:
template <class T>
struct HasConvertionTo;

template <>
struct HasConvertionTo<long{};

template <>
struct HasConvertionTo<bool{};

public:
template <class T>
operator T () const
{
return Convert(HasConvertionTo<T>()); // CT error trigger
}

private:
template <class T>
T Convert(HasConvertionTo<T>) const // dispatcher
{
return Convert(T());
}

long Convert(long) const
{
return 10;
}

bool Convert(bool) const
{
return true;
}
};

int main()
{
A a;

long l = a;

if (a) {
}
}
--
Thanks
Barry
Sep 30 '07 #1
2 7671
Barry wrote:
[STD
14.7.3 Explicit specialization [temp.expl.spec]
An explicit specialization of any of the following:
¡ª function template
¡ª class template
¡ª member function of a class template
¡ª static data member of a class template
¡ª member class of a class template
¡ª member class template of a class template
¡ª member function template of a class template
can be declared by a declaration introduced by template<>

End-STD]

if I didn't get it wrong, we can have member class template explicit
sorry, typo, can't
specialization if the outter class is not templated. Right?

--
Thanks
Barry
Sep 30 '07 #2
Barry wrote:
The following code compiles with VC8
but fails to compiles with Comeau online,

[Error
"ComeauTest.c", line 7: error: explicit specialization is not allowed in
the
current scope
template <>
^

"ComeauTest.c", line 10: error: explicit specialization is not allowed
in the
current scope
template <>
^
End-Error]
I locate the standard here:

[STD
14.7.3 Explicit specialization [temp.expl.spec]
An explicit specialization of any of the following:
¡ª function template
¡ª class template
¡ª member function of a class template
¡ª static data member of a class template
¡ª member class of a class template
¡ª member class template of a class template
¡ª member function template of a class template
can be declared by a declaration introduced by template<>

End-STD]

if I didn't get it wrong, we can have member class template explicit
specialization if the outter class is not templated. Right?
///////////////////////////////////////////

struct A
{
private:
template <class T>
struct HasConvertionTo;

template <>
struct HasConvertionTo<long{};

template <>
struct HasConvertionTo<bool{};

public:
template <class T>
operator T () const
{
return Convert(HasConvertionTo<T>()); // CT error trigger
}

private:
template <class T>
T Convert(HasConvertionTo<T>) const // dispatcher
{
return Convert(T());
}

long Convert(long) const
{
return 10;
}

bool Convert(bool) const
{
return true;
}
};

int main()
{
A a;

long l = a;

if (a) {
}
}
14.7.3 Explicit specialization

2.
An explicit specialization of a member function, member class or static
data member of a class template
shall be declared in the namespace of which the class template is a
member. Such a declaration may also
be a definition. If the declaration is not a definition, the
specialization may be defined later in the namespace
in which the explicit specialization was declared, or in a namespace
that encloses the one in which
the explicit specialization was declared.

So I think the code should be written in this way:

[Code --
struct A
{
private:
template <class T>
struct HasConvertionTo;

public:
template <class T>
operator T () const
{
return Convert(HasConvertionTo<T>()); // CT error trigger
}

private:
template <class T>
T Convert(HasConvertionTo<T>) const // dispatcher
{
return Convert(T());
}

long Convert(long) const
{
return 10;
}

bool Convert(bool) const
{
return true;
}
};

template <>
struct A::HasConvertionTo<long{};

template <>
struct A::HasConvertionTo<bool{};
-- End-Code]

Which Comeau accepts.

--
Thanks
Barry
Sep 30 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2562
by: SainTiss | last post by:
Hi, From what I've read in several places, it seems that explicit specialization of member functions of class templates is allowed, but partial specialization isn't: template<class T, class...
4
1816
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
0
1471
by: Patrick Kowalzick | last post by:
Dear all, the following code is illegeal (but compiles with MSVC 7.1): // *** illegal *** struct outer0 { template<typename inner_var> struct inner { }; template<>
11
1958
by: Gernot Frisch | last post by:
// variable creation object template <class T> class CL { public: CL (const T& t) {} template <> CL<double>(const T& t) {} }; How do I make a constructor for T = double, ..., so I can...
5
6563
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
8
4774
by: Ferdi Smit | last post by:
I've never understood the rationale of allowing partial, but not explicit specialization for classes at non-namespace scope. Ie.: struct A { template <typename T1, typename T2> struct B {}; ...
5
2635
by: henkoo | last post by:
i want to explicit instantiate a member function of a class template, and got some error, can anyone give some advice to correct it? 3x complier: g++ 3.2 #include <iostream> #include...
2
1843
by: jason.cipriani | last post by:
Again I find myself bogged down in template syntax. Here I have a class with a template member function. I know ahead of time what template parameters I will be passing to the function. I do not...
0
2873
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
7051
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7054
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4493
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
567
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.