Topic: const template type arguments
Author: Srinivas Vobilisetti <srinivas-v@usa.net>
Date: 1998/04/17 Raw View
Michael Jay Lippert wrote:
>
> template <class T>
> class TP
> {
> public:
> TP( T x ) : m_x( x )
> {
> }
> T& GetObj()
> {
> return m_x;
> }
> T m_x;
> };
>
> typedef const int const_int;
>
> // elsewhere...
>
> TP<int> p1( 4 );
> TP<const_int> p2( 3 );
>
> // No surprise this works
> p1.GetObj() = p2.GetObj();
>
> // I was hoping p2's object was const...
> // but, the compiler is pretty happy with this
> p2.GetObj() = p1.GetObj();
>
> It appears that TP<int> and TP<const int> are both instantiated as
> TP<int>.
Use a static data member (of type int) and increment it in the
constructor. After the two instances each for TP<int> and TP<const int>
are defined, print the value of the static data member of both TP<int>
and TP<const int> to see if the compiler is using the same instantiation
for both TP<int> and TP<const int> which it should not.
> I'm sure there is a perfectly good explanation for this, but since
> I'm not pretending to be the guru of templates, I figured I'd "ask
> around."
>
I think, your compiler is broken. It should give an error because what
p2.GetObj() returns is a 'const int &' which is NOT a lvalue. So, it
should NOT appear on the left hand side of the assignment operator.
Srinivas
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Choi Hojin" <pynoos@in4bank.com>
Date: 1998/04/17 Raw View
Michael Jay Lippert ( ) <6h46h0$a3d$1@ndnws01.ne.highway1.com>
...
>=====================
>Has anyone tried to use a const type as a template argument had any
>success? I was attempting to define an auto ref count pointer that
>served as an alias to a pointer to const object, and discovered that
>it didn't work.
>
>The following code snippet illustrates the situation:
>
>
>template <class T>
>class TP
>{
>public:
> TP( T x ) : m_x( x )
> {
> }
> T& GetObj()
> {
> return m_x;
> }
> T m_x;
>};
>
>typedef const int const_int;
>
>// elsewhere...
>
> TP<int> p1( 4 );
> TP<const_int> p2( 3 );
>
> // No surprise this works
> p1.GetObj() = p2.GetObj();
>
> // I was hoping p2's object was const...
> // but, the compiler is pretty happy with this
> p2.GetObj() = p1.GetObj();
>
>
>It appears that TP<int> and TP<const int> are both instantiated as
>TP<int>.
> I'm sure there is a perfectly good explanation for this, but since
>I'm not pretending to be the guru of templates, I figured I'd "ask
>around."
I don't know exactly why it work such that!
But when you try to find the name from runtime type info, you can get the
same name from both... code and run next codes..
It shows template does not take the const type identifier..
so your example works fine...
Hmm.. Let me think more...
Thanx for like this good question..
C U...
#include <fstream.h>
#include <typeinfo.h>
template <class T>
class TP
{
public:
TP( T x ) : m_x( x )
{
}
T& GetObj()
{
return m_x;
}
T m_x;
};
typedef const int const_int;
void main()
{
TP<int> p1( 4 );
TP<const_int> p2( 3 );
cout << typeid(p1).name() << endl;
cout << typeid(p2).name() << endl;
cout << (typeid(p2.GetObj()) == typeid(p1.GetObj())) << endl;
}
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <bonnardv@pratique.fr>
Date: 1998/04/19 Raw View
Srinivas Vobilisetti <srinivas-v@usa.net> writes:
> I think, your compiler is broken.
You are probably right.
> It should give an error because what
> p2.GetObj() returns is a 'const int &' which is NOT a lvalue. So, it
> should NOT appear on the left hand side of the assignment operator.
Just a minor nit: const int& is the type of a lvalue, which
is not assignable, not a rvalue.
--
Valentin Bonnard mailto:bonnardv@pratique.fr
info about C++/a propos du C++: http://pages.pratique.fr/~bonnardv/
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: mlippertSP.AM@gis.net (Michael Jay Lippert)
Date: 1998/04/16 Raw View
A colleague sent me the following question about const template type
arguments (see below).
I realized that I really didn't know the answer myself so I started
looking in Stoustrup's book "C++ Programming language 3rd edition 1st
printing", figuring that it was the most up to date authority on the
C++ language (at least until there is a new edition of the ARM :-).
The examples in sec 13.2.4 (Templates:Type equivalence) don't refer to
const at all, so that section was no help.
The chapter on Types doesn't discuss const except as a declarator
operator (in sec 4.9.1), nor does it discuss volatile, for that
matter, so I still don't know if applying those modifiers to a type is
a "new" type.
Then in sec 13.5.2 when discussing template specialization, an example
of a specialization of the template function less is given for the
type "const char*". This of course implies that const does create a
distinct type for template instantiation. However, the text implies
that this specialization would be used instead of creating a new
instantiation for the type "char*", thus implying that const doesn't
matter.
The upshot is that I am still confused and would appreciate an
explanation of how this is supposed to work, so I could determine if
the behavior he experienced is a compiler (MSVC 5) "bug", or working
as specified by the language.
Thanx for taking the time to look at this.
Michael Lippert
=====================
Has anyone tried to use a const type as a template argument had any
success? I was attempting to define an auto ref count pointer that
served as an alias to a pointer to const object, and discovered that
it didn't work.
The following code snippet illustrates the situation:
template <class T>
class TP
{
public:
TP( T x ) : m_x( x )
{
}
T& GetObj()
{
return m_x;
}
T m_x;
};
typedef const int const_int;
// elsewhere...
TP<int> p1( 4 );
TP<const_int> p2( 3 );
// No surprise this works
p1.GetObj() = p2.GetObj();
// I was hoping p2's object was const...
// but, the compiler is pretty happy with this
p2.GetObj() = p1.GetObj();
It appears that TP<int> and TP<const int> are both instantiated as
TP<int>.
I'm sure there is a perfectly good explanation for this, but since
I'm not pretending to be the guru of templates, I figured I'd "ask
around."
--
Mike Lippert
Turning Point Software
mlippert@gis.net
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1998/04/16 Raw View
Michael Jay Lippert writes:
> the behavior he experienced is a compiler (MSVC 5) "bug", or working
> as specified by the language.
> It appears that TP<int> and TP<const int> are both instantiated as
> TP<int>.
It is a bug. egcs 1.0.2 rejects the invalid assignment.
--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
http://www.dcc.unicamp.br/~oliva
Universidade Estadual de Campinas, SP, Brasil
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]