Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-03-2008, 05:12 AM
nomad
 
Posts: n/a
Specialization of inner templates: should it be legal?

Hello All,

My question concerns the specialization of inner templates without the
specialization of outer templates, for example

template<class T1>
struct A
{
template<class T2> struct B;
template<> struct B<int> {}; // error
template<> struct B<double> {}; // error
};

Well we know that the standard emphatically says this is not allowed
in 14.7.3.18

I would like to understand the rationale behind this requirement.

One argument I read on this forum goes like

template<class T1>
struct A
{
template<class T2> struct B;
template<> struct B<int> {}; // lets say is allowed
template<> struct B<double> {}; // lets say is allowed
};

template<class int> struct A {};

Now the compiler is confused on A<int>::B<int>. This is illogical.
Because clearly there is no B in A<int> and hence is an error.

For the curious, this restriction can be overcome as follows

struct true_ {};

template<class T1, class T2> struct if_same {};
template<class T> struct if_same<T, T> { typedef true_ type; };

template<class T1>
struct A
{
template<class T2, class = true_> struct B;
template<class T2> struct B<T2, typename if_same<T2,int>::type>
{ int x; };
template<class T2> struct B<T2, typename if_same<T2,double>::type>
{ double x; };
};

This works on both GCC 4.3.0 and ICC 10.1.012. Although, I think this
should not be legal per 14.7.3.18.

Can someone be so nice to shed some light on the rationale behind
14.7.3.18?

thanks,
Krishna.

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

Reply With Quote
  #2 (permalink)  
Old 10-05-2008, 08:57 PM
Faisal Vali
 
Posts: n/a
Re: Specialization of inner templates: should it be legal?

On Oct 2, 11:12 pm, nomad <gkmo...@gmail.com> wrote:
> My question concerns the specialization of inner templates without the
> specialization of outer templates, for example
>
> template<class T1>
> struct A
> {
> template<class T2> struct B;
> template<> struct B<int> {}; // error
> template<> struct B<double> {}; // error
>
> };
>
> Well we know that the standard emphatically says this is not allowed
> in 14.7.3.18
>
> I would like to understand the rationale behind this requirement.
>


I have yet to see a good reason for this - appears to be just another
inconsistency between explicit vs partial specialization syntax that
no one has submitted as a defect to the standards committee (did i
miss the DR?)

<snip>

> For the curious, this restriction can be overcome as follows
>
> struct true_ {};
>
> template<class T1, class T2> struct if_same {};
> template<class T> struct if_same<T, T> { typedef true_ type; };
>
> template<class T1>
> struct A
> {
> template<class T2, class = true_> struct B;
> template<class T2> struct B<T2, typename if_same<T2,int>::type>
> { int x; };
> template<class T2> struct B<T2, typename if_same<T2,double>::type>
> { double x; };
>
> };
>
> This works on both GCC 4.3.0 and ICC 10.1.012. Although, I think this
> should not be legal per 14.7.3.18.


I do not see anything in 14.7.3.18 that forbids the above - What
specific phrase/sentence do you feel does or should?

Also, I do not think you need to resort to SFINAE to select the best
specialization here - it might be simpler to just use ESABTL ;)
(Explicitly Specialize All But The Last, which scales well with
multiple parameters that need to be fully specialized):

template<class T> struct Outer {
template<class, class = char, bool = false> struct Inner;

private: // sorry ;)
template<class T2, class T3> struct Inner<T2,T3,true>; // do not try
your tricks -

template<bool _> struct Inner<int,char,_> {
typedef int int_type;
};
template<bool _> struct Inner<double,char,_> {
typedef double double_type;
};
template<bool _> struct Inner<int,Outer<int>,_> {
typedef int outer_int_type;
};

};

Outer<int>::Inner<int>::int_type x;
Outer<int>::Inner<double>::double_type x2;
Outer<int>::Inner<int,Outer<int>>::outer_int_type x3;


>
> Can someone be so nice to shed some light on the rationale behind
> 14.7.3.18?


good luck - I hope they do ;)

regards,
Faisal Vali
Radiation Oncology
Loyola

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

Reply With Quote
  #3 (permalink)  
Old 10-06-2008, 08:59 PM
nomad
 
Posts: n/a
Re: Specialization of inner templates: should it be legal?

Dear Faisal Vali,

> > My question concerns the specialization of inner templates without the
> > specialization of outer templates, for example

>
> > template<class T1>
> > struct A
> > {
> > template<class T2> struct B;
> > template<> struct B<int> {}; // error
> > template<> struct B<double> {}; // error

>
> > };

>
> > Well we know that the standard emphatically says this is not allowed
> > in 14.7.3.18

>
> > I would like to understand the rationale behind this requirement.

>
> I have yet to see a good reason for this - appears to be just another
> inconsistency between explicit vs partial specialization syntax that
> no one has submitted as a defect to the standards committee (did i
> miss the DR?)



How easy is it to submit a defect report? If it is not too difficult I
would not mind spending some time on it.


> > For the curious, this restriction can be overcome as follows

>
> > struct true_ {};

>
> > template<class T1, class T2> struct if_same {};
> > template<class T> struct if_same<T, T> { typedef true_ type; };

>
> > template<class T1>
> > struct A
> > {
> > template<class T2, class = true_> struct B;
> > template<class T2> struct B<T2, typename if_same<T2,int>::type>
> > { int x; };
> > template<class T2> struct B<T2, typename if_same<T2,double>::type>
> > { double x; };

>
> > };

>
> > This works on both GCC 4.3.0 and ICC 10.1.012. Although, I think this
> > should not be legal per 14.7.3.18.

>
> I do not see anything in 14.7.3.18 that forbids the above - What
> specific phrase/sentence do you feel does or should?



You are right. There is nothing in it that prevents it.


> Also, I do not think you need to resort to SFINAE to select the best
> specialization here - it might be simpler to just use ESABTL ;)
> (Explicitly Specialize All But The Last, which scales well with
> multiple parameters that need to be fully specialized):
>
> template<class T> struct Outer {
> template<class, class = char, bool = false> struct Inner;
>
> private: // sorry ;)
> template<class T2, class T3> struct Inner<T2,T3,true>; // do not try
> your tricks -
>
> template<bool _> struct Inner<int,char,_> {
> typedef int int_type;
> };
> template<bool _> struct Inner<double,char,_> {
> typedef double double_type;
> };
> template<bool _> struct Inner<int,Outer<int>,_> {
> typedef int outer_int_type;
> };
>
> };
>
> Outer<int>::Inner<int>::int_type x;
> Outer<int>::Inner<double>::double_type x2;
> Outer<int>::Inner<int,Outer<int>>::outer_int_type x3;



I like your solution, it is much easier to read. I think you missed a
public: before the partial specializations.


> > Can someone be so nice to shed some light on the rationale behind
> > 14.7.3.18?

>
> good luck - I hope they do ;)



I hope they do not. Otherwise it will be a pain in the ass to use the
workarounds. Not to mention the annoying superfluous template argument
floating all around the debug output.

cheers,
Krishna.

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

Reply With Quote
  #4 (permalink)  
Old 10-07-2008, 03:31 AM
Faisal Vali
 
Posts: n/a
Re: Specialization of inner templates: should it be legal?

On Oct 6, 2:59 pm, nomad <gkmo...@gmail.com> wrote:
> Dear Faisal Vali,
>

<snip>
> How easy is it to submit a defect report? If it is not too difficult I
> would not mind spending some time on it.


It used to be that you just had to send a post to comp.std.c++ - but
since that
group is dead - i do not know the proper procedure.

What I did do was submit the DR to William Miller of EDG since he
seems to be the
person in charge of the list (this info is available off the c++
standards website) - he acknowledged receipt and stated that while it
was too late to be a part
of this next revision of DRs - it will be included in the next to next
revision for consideration.


<snip>

> > template<class T> struct Outer {
> > template<class, class = char, bool = false> struct Inner;

>
> > private: // sorry ;)
> > template<class T2, class T3> struct Inner<T2,T3,true>; // do not try
> > your tricks -

>
> > template<bool _> struct Inner<int,char,_> {
> > typedef int int_type;
> > };
> > template<bool _> struct Inner<double,char,_> {
> > typedef double double_type;
> > };
> > template<bool _> struct Inner<int,Outer<int>,_> {
> > typedef int outer_int_type;
> > };

>
> > };

>
> > Outer<int>::Inner<int>::int_type x;
> > Outer<int>::Inner<double>::double_type x2;
> > Outer<int>::Inner<int,Outer<int>>::outer_int_type x3;

>
> I like your solution, it is much easier to read. I think you missed a
> public: before the partial specializations.


Not really - the private: was intentional - I added it to stimulate
discussion.
I do not know what the standard says about providing different access
to the
primary template vs its specializations - but comeau compiles the
above code.

I have always thought that access checking was done as the last step
when binding names - i.e. after overload resolution (and i also
assumed after selecting the partial specialization).
But Comeau compiles the above code, suggesting that access is only
checked for the primary template?
If anyone can find something in the standard that makes this code ill-
formed, I sure would like to know.

>
> > > Can someone be so nice to shed some light on the rationale behind
> > > 14.7.3.18?

>
> > good luck - I hope they do ;)

>
> I hope they do not. Otherwise it will be a pain in the ass to use the
> workarounds. Not to mention the annoying superfluous template argument
> floating all around the debug output.


I agree - lets see what happens with the DR I submitted.

regards,
Faisal Vali
Radiation Oncology
Loyola


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

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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



All times are GMT. The time now is 03:53 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Mortgages | Cell Phones | Remortgaging | Remortgages | Advertising



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114