Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Explicit specialization of member class template

Reply
Thread Tools

Explicit specialization of member class template

 
 
Barry
Guest
Posts: n/a
 
      09-30-2007
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
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      09-30-2007
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
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      09-30-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of non-template member of template class Noah Roberts C++ 6 02-02-2011 09:53 PM
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
Explicit specialization of member template when type is anothertemplate type. jason.cipriani@gmail.com C++ 5 12-05-2008 05:33 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments