C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

const T specialization

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Michael v. Szombathely
Guest





PostPosted: Sat Sep 27, 2003 1:04 pm    Post subject: const T specialization Reply with quote



Dear moderators,

for some reasons my posting two weeks ago (see below) did not reached its
aim. Thus - following the guidelines - I (re)send it to this address.

Regards Michael

-- cut here ------------------------------------------------------------------

Hi,

we are faced with an strange ambiguity problem, while porting the
Gtkmm-Library to the recent Sun Studio ONE 8 compiler under
Solaris. The compiler denies to compile the following code, while g++
(3.2.3), Comeau (4.3.3beta test drive) and older Sun compilers accept it.

The basic question of this example is, whether const T is more
specialised than T (or not) during partial ordering of template
spezialization. According to the C++ standard, which in 14.8.2.1 says
that top level CV qualifiers are ignored for type deduction, the
recent Sun compiler seems to be right. Do I miss something here?

$ cat foo.cc
#include <iostream>

template <class T> class Foo {};

template <typename T> class Traits {
public:
const char* whoami() {
return "generic template";
}
};

template <typename T> class Traits<Foo {
public:
const char* whoami() {
return "partial specialization for Foo<T>";
}
};

template <typename T> class Traits<Foo {
public:
const char* whoami() {
return "partial specialization for Foo<const T>";
}
};

int main(int, char*[])
{
Traits<int> it;
Traits<Foo fit;
Traits<Foo cfit;

std::cout << "Traits "
<< it.whoami() << std::endl;
std::cout << "Traits --> "
<< fit.whoami() << std::endl;
std::cout << "Traits --> "
<< cfit.whoami() << std::endl;

return 0;
}
$ CC foo.cc
"foo.cc", line 26: Error: Ambiguous partial specialization for Traits, Traits and Traits.
"foo.cc", line 26: Error: Ambiguous partial specialization for Traits<Foo, Traits and Traits.
"foo.cc", line 27: Error: Ambiguous partial specialization for Traits<Foo, Traits and Traits.
"foo.cc", line 27: Error: Ambiguous partial specialization for Traits<Foo, Traits and Traits.
"foo.cc", line 30: Error: Ambiguous partial specialization for Traits<Foo, Traits and Traits.
"foo.cc", line 31: Error: Ambiguous partial specialization for Traits<Foo, Traits and Traits.
6 Error(s) detected.
$ CC -V
CC: Sun C++ 5.5 Patch 113817-01 2003/05/18

Other compilers, which compile the code, give the following output:

Traits<int> --> generic template
Traits<Foo --> partial specialization for Foo<T>
Traits<Foo --> partial specialization for Foo<const T>


Thanks in advance,

Michael v. Szombathely
<szombath (AT) web (DOT) de>


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Siemel Naran
Guest





PostPosted: Mon Sep 29, 2003 6:20 am    Post subject: Re: const T specialization Reply with quote



"Michael v. Szombathely" <szombath (AT) web (DOT) de> wrote in message

Quote:
The basic question of this example is, whether const T is more
specialised than T (or not) during partial ordering of template
spezialization. According to the C++ standard, which in 14.8.2.1 says
that top level CV qualifiers are ignored for type deduction, the
recent Sun compiler seems to be right. Do I miss something here?

I don't know who is right. But I asked the question on this newsgroup once
and got a workaround. The original code was


template <class T>
struct no_toplevel_const
{
typedef typename helper::no_toplevel_const<T*>::result result;
};

template <class T>
struct no_toplevel_const<const T>
{
typedef T result;
};


The workaround is

namespace helper
{
template <class T>
struct no_toplevel_const
{
typedef T result;
};

template <class T>
struct no_toplevel_const<T const *>
{
typedef T result;
};
}

template <class T>
struct no_toplevel_const
{
typedef typename helper::no_toplevel_const<T*>::result result;
};


Maybe you can apply it to your own code.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Richard Corden
Guest





PostPosted: Mon Sep 29, 2003 9:31 pm    Post subject: Re: const T specialization Reply with quote




Hi,

"Michael v. Szombathely" <szombath (AT) web (DOT) de> writes:

Quote:
The basic question of this example is, whether const T is more
specialised than T (or not) during partial ordering of template
spezialization. According to the C++ standard, which in 14.8.2.1 says
that top level CV qualifiers are ignored for type deduction, the
recent Sun compiler seems to be right. Do I miss something here?

I think the folowing might explain it:

14.5.2.5 (Partial ordering of class template specializations) says
that we see which partial spec is better using 14.5.5.2 (Partial
ordering of function templates).

14.5.5.2/4 says after we've deduced the arguments, we compare the
specializations and one is at least as specialized as the other if,
and only if, the deduction succeeds and the deduced parameter types
are an exact match (so the deduction does not rely on implicit
conversions).

So, I think that 14.8.2.1 still holds, and both templates can have
their arguments deduced, but ordering should choose the appropriate
partial spec where the arguments are an exact match.

I hope this helps,

Regards,

Richard


--
Richard Corden
To reply remove 's' from address

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Momchil Velikov
Guest





PostPosted: Mon Sep 29, 2003 9:54 pm    Post subject: Re: const T specialization Reply with quote

"Michael v. Szombathely" <szombath (AT) web (DOT) de> wrote

Quote:
According to the C++ standard, which in 14.8.2.1 says
that top level CV qualifiers are ignored for type deduction

Template paremeter type deduction allows for cv-adjusments,
but partial order is defined in the terms of an exact match.

Quote:
the
recent Sun compiler seems to be right. Do I miss something here?

$ cat foo.cc
#include <iostream

template
template <typename T> class Traits {
public:
const char* whoami() {
return "generic template";
}
};

template <typename T> class Traits<Foo {
public:
const char* whoami() {
return "partial specialization for Foo<T>";
}
};

template <typename T> class Traits<Foo {
public:
const char* whoami() {
return "partial specialization for Foo<const T>";
}
};

Per 14.5.4.2, partial ordering of the specializations is equivalent to
partial ordering of the the following function templates, respectively:

I. f (X<T>)
II. f (X<Foo)
III. f (X<Foo)

Per 14.5.5.2, the function templates are ordered as follows:

a) II is more specialized than I, because X<Foo matches X<T>,
whereas X<A> does not match X<Foo

b) III is more specialized than I, because X<Foo matches X<T>,
whereas X<A> does not match, X<Foo

c) III is more specialized than II, because X<Foo matches X<Foo,
whereas X<Foo does not match X<Foo

IOW, we have ordering defined for each pair of template partial specializations
and no ambiguity.

~velco

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.