![]() |
|
|
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. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
|
|||
|
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! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|