 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jing Chen Guest
|
Posted: Tue Mar 29, 2005 9:33 am Post subject: const object instantiation |
|
|
All:
I don't understand why it is different to instantiate template using const
object and non-const object.
template<typename T,typename SubType = void>
class TestTemplate
{
public:
static void showType()
{
FMK_TRACE("TestTemplate::showType with explict instantion");
}
}; // A
template<typename T>
class TestTemplate<T,void>
{
public:
static void showType()
{
FMK_TRACE("TestTemplate::showType with patial instantion");
}
}; //B
struct myStruct
{
};
int main()
{
typedef TestTemplate<const myStruct> myConstObj; //1
myConstObj::showType();
typedef TestTemplate<myStruct> myObj; //2
myObj::showType();
}
in SUN solaris 9 with C++ compiler Forte 7, 1 was instantiated from full
specialization of template(A), while 2 was instantiated from partial
specialization of template(B). while use Forte 6.1 both are instantiated
from partial specialization (B). why they are different?
thanks
jing
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Shantanu Garg Guest
|
Posted: Tue Mar 29, 2005 10:03 pm Post subject: Re: const object instantiation |
|
|
| Quote: | int main()
{
typedef TestTemplate<const myStruct> myConstObj; //1
myConstObj::showType();
typedef TestTemplate<myStruct> myObj; //2
myObj::showType();
}
in SUN solaris 9 with C++ compiler Forte 7, 1 was instantiated from full
specialization of template(A), while 2 was instantiated from partial
specialization of template(B). while use Forte 6.1 both are instantiated
from partial specialization (B). why they are different?
|
I think your compiler is wrong. In both the cases Version B of the
function should be called. I think 'const' shouldn't have any affect on
template instantiation.
-Shantanu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
annamalai.gurusami@gmail. Guest
|
Posted: Thu Mar 31, 2005 2:10 pm Post subject: Re: const object instantiation |
|
|
Jing Chen wrote:
(snip)
| Quote: | template
class TestTemplate
{
public:
static void showType()
{
FMK_TRACE("TestTemplate::showType with patial instantion");
}
}; //B
|
It is better called as "partial specialization" rather than "partial
instantiation". Also, when the above template class is called without
specifying the second template argument the default is void, which
will then call the template class that has been specialized with void
for
second argument.
So both the times, the partially specialized template should be called.
This behaviour is exhibited by g++ 3.3.4.
Also, since the const qualifier is applied to the first template
argument and the partial specialization is done with the second
argument,
I doubt whether this behaviour is related to the const qualifier.
(Maybe,
I will try to reproduce similar stranger behaviour without using
const!)
BTW, I haven't read the C++ standards about this. But what I am
stating
is my understanding of C++ and what its "logical" behaviour would be!
Rgds,
anna
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
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
|
|