problem with explicit template instantiation in Visual C++ 6.0 . 
July 22nd, 2005, 06:08 AM
| | | problem with explicit template instantiation in Visual C++ 6.0 .
/*
Hi,
I have a problem with explicit instantiation of templates in Visual
C++ 6.0.
I have provided the source below. I have an example of a function
template that produces incorrect output in the code below.
The function
template < typename T >
void DummyFunctionDoesNotWork(T &o);
Produces incorrect output. The following is the expected output:
First Type
Second Type
But, instead, I obtain the following is the output:
Second Type
Second Type
I have written a second function
template < typename T >
void DummyFunctionWorks(T &o);
That produces the correct output.
Could someone please test the code given below in Visual C++.NET
and let me know if it works?
Thanks,
CCarbonera
*/
#include <fstream.h>
enum explicit_t
{first_t=1,
second_t=0};
template <explicit_t t>
struct Dummy
{
const char* operator()(void){ return " UNDEFINED "; };
};
template <>
const char* Dummy< first_t >::operator()(){ return " First Type "; }
template <>
const char* Dummy<second_t>::operator()(){ return " Second Type "; }
template < typename T >
void DummyFunctionWorks(T &o)
{ cout << o() << endl; }
template < typename T >
void DummyFunctionDoesNotWork()
{ T o; cout << o() << endl; }
void main(int argc, char* argv[])
{
cout<< "The following produces the correct output:" << endl;
Dummy<first_t> frst;
Dummy<second_t> scnd;
DummyFunctionWorks < Dummy< first_t > > ( frst );
DummyFunctionWorks < Dummy< second_t > > ( scnd );
cout<< "The following produces the wrong output:" << endl;
DummyFunctionDoesNotWork < Dummy < first_t > > ( );
DummyFunctionDoesNotWork < Dummy < second_t > > ( );
} | 
July 22nd, 2005, 06:08 AM
| | | Re: problem with explicit template instantiation in Visual C++ 6.0 .
C. Carbonera wrote:[color=blue]
> /*[/color]
[snip][color=blue]
>
> Could someone please test the code given below in Visual C++.NET
> and let me know if it works?[/color]
It work in VC++ 2003 with a few minor modifications to include the correct
header:
[color=blue]
>
> Thanks,
> CCarbonera
> */
> #include <fstream.h>[/color]
#include <fstream>
using namespace std;
[color=blue]
>
> enum explicit_t
> {first_t=1,
> second_t=0};
>
> template <explicit_t t>
> struct Dummy
> {
> const char* operator()(void){ return " UNDEFINED "; };
> };
>
> template <>
> const char* Dummy< first_t >::operator()(){ return " First Type "; }
>
> template <>
> const char* Dummy<second_t>::operator()(){ return " Second Type "; }
>
> template < typename T >
> void DummyFunctionWorks(T &o)
> { cout << o() << endl; }
>
> template < typename T >
> void DummyFunctionDoesNotWork()
> { T o; cout << o() << endl; }
>
> void main(int argc, char* argv[])
> {
> cout<< "The following produces the correct output:" << endl;
> Dummy<first_t> frst;
> Dummy<second_t> scnd;
> DummyFunctionWorks < Dummy< first_t > > ( frst );
> DummyFunctionWorks < Dummy< second_t > > ( scnd );
>
> cout<< "The following produces the wrong output:" << endl;
> DummyFunctionDoesNotWork < Dummy < first_t > > ( );
> DummyFunctionDoesNotWork < Dummy < second_t > > ( );
> }[/color] | 
July 22nd, 2005, 06:08 AM
| | | Re: problem with explicit template instantiation in Visual C++ 6.0 .
[snip][color=blue]
>
> #include <fstream>[/color]
Sorry, this should be <iostream> , not <fstream> .
[color=blue]
> using namespace std;
>[/color]
[snip | 
July 22nd, 2005, 06:09 AM
| | | Re: problem with explicit template instantiation in Visual C++ 6.0 .
"C. Carbonera" <ccarbonera@msn.com> wrote in message
news:dfb2f99f.0402041520.7e5f9fa5@posting.google.c om...[color=blue]
> /*
> Hi,
>
> I have a problem with explicit instantiation of templates in Visual
> C++ 6.0.
> I have provided the source below. I have an example of a function
> template that produces incorrect output in the code below.
>
> The function
> template < typename T >
> void DummyFunctionDoesNotWork(T &o);
>
> Produces incorrect output. The following is the expected output:
> First Type
> Second Type
>
> But, instead, I obtain the following is the output:
> Second Type
> Second Type
>
> I have written a second function
>
> template < typename T >
> void DummyFunctionWorks(T &o);
>
> That produces the correct output.
>
> Could someone please test the code given below in Visual C++.NET
> and let me know if it works?
>
> Thanks,
> CCarbonera
> */
> #include <fstream.h>
>
> enum explicit_t
> {first_t=1,
> second_t=0};
>
> template <explicit_t t>
> struct Dummy
> {
> const char* operator()(void){ return " UNDEFINED "; };
> };
>
> template <>
> const char* Dummy< first_t >::operator()(){ return " First Type "; }
>
> template <>
> const char* Dummy<second_t>::operator()(){ return " Second Type "; }
>
> template < typename T >
> void DummyFunctionWorks(T &o)
> { cout << o() << endl; }
>
> template < typename T >
> void DummyFunctionDoesNotWork()
> { T o; cout << o() << endl; }
>
> void main(int argc, char* argv[])
> {
> cout<< "The following produces the correct output:" << endl;
> Dummy<first_t> frst;
> Dummy<second_t> scnd;
> DummyFunctionWorks < Dummy< first_t > > ( frst );
> DummyFunctionWorks < Dummy< second_t > > ( scnd );
>
> cout<< "The following produces the wrong output:" << endl;
> DummyFunctionDoesNotWork < Dummy < first_t > > ( );
> DummyFunctionDoesNotWork < Dummy < second_t > > ( );
> }[/color]
This is a very funny bug! Have you tried reversing the last two lines?
I get:
First Type
First Type
Both calls are resolved differently, juist because they are invoked in
a different order.
(By the way, you are really talking about explicit specializaion, not
explicit instantiation. Also main returns int, ... )
Jonathan | 
July 22nd, 2005, 06:10 AM
| | | Re: problem with explicit template instantiation in Visual C++ 6.0 .
"Jonathan Turkanis" <technews@kangaroologic.com> wrote in message news:<bvsgor$vs42v$1@ID-216073.news.uni-berlin.de>...[color=blue]
> "C. Carbonera" <ccarbonera@msn.com> wrote in message
> news:dfb2f99f.0402041520.7e5f9fa5@posting.google.c om...[color=green]
> > /*
> > Hi,
> >
> > I have a problem with explicit instantiation of templates in Visual
> > C++ 6.0.
> > I have provided the source below. I have an example of a function
> > template that produces incorrect output in the code below.
> >
> > The function
> > template < typename T >
> > void DummyFunctionDoesNotWork(T &o);
> >
> > Produces incorrect output. The following is the expected output:
> > First Type
> > Second Type
> >
> > But, instead, I obtain the following is the output:
> > Second Type
> > Second Type
> >
> > I have written a second function
> >
> > template < typename T >
> > void DummyFunctionWorks(T &o);
> >
> > That produces the correct output.
> >
> > Could someone please test the code given below in Visual C++.NET
> > and let me know if it works?
> >
> > Thanks,
> > CCarbonera
> > */
> > #include <fstream.h>
> >
> > enum explicit_t
> > {first_t=1,
> > second_t=0};
> >
> > template <explicit_t t>
> > struct Dummy
> > {
> > const char* operator()(void){ return " UNDEFINED "; };
> > };
> >
> > template <>
> > const char* Dummy< first_t >::operator()(){ return " First Type "; }
> >
> > template <>
> > const char* Dummy<second_t>::operator()(){ return " Second Type "; }
> >
> > template < typename T >
> > void DummyFunctionWorks(T &o)
> > { cout << o() << endl; }
> >
> > template < typename T >
> > void DummyFunctionDoesNotWork()
> > { T o; cout << o() << endl; }
> >
> > void main(int argc, char* argv[])
> > {
> > cout<< "The following produces the correct output:" << endl;
> > Dummy<first_t> frst;
> > Dummy<second_t> scnd;
> > DummyFunctionWorks < Dummy< first_t > > ( frst );
> > DummyFunctionWorks < Dummy< second_t > > ( scnd );
> >
> > cout<< "The following produces the wrong output:" << endl;
> > DummyFunctionDoesNotWork < Dummy < first_t > > ( );
> > DummyFunctionDoesNotWork < Dummy < second_t > > ( );
> > }[/color]
>
> This is a very funny bug! Have you tried reversing the last two lines?
> I get:
>
> First Type
> First Type
>
> Both calls are resolved differently, juist because they are invoked in
> a different order.
>
> (By the way, you are really talking about explicit specializaion, not
> explicit instantiation. Also main returns int, ... )
>
> Jonathan[/color]
Jonathan,
Thank you for your feedback.
I have one comment. The problem occurs when the functions
void DummyFunctionDoesNotWork < Dummy < first_t > > ( ) and
void DummyFunctionDoesNotWork < Dummy < second_t > > ( ) are
instantiated;
hence, I labeled the problem as an eplicit instantiation.
There explicit specializations of the functions
const char* Dummy< first_t >::operator()() and
const char* Dummy< second_t >::operator()()
work well as the example below illustrates.
/*
Please, turn on and off the compiler directive '#define WORKS' below
to compare the results.
*/
#include <iostream>
using namespace std;
enum explicit_t
{
first_t=1,
second_t=0
};
template <explicit_t t>
struct Dummy{ virtual const char* operator()(void){ return NULL; };
};
template<>
const char* Dummy<first_t>::operator()(void) { return "First
Function"; }
template<>
const char* Dummy<second_t>::operator()(void) { return "Second
Function"; }
//#define WORKS
#ifdef WORKS
template <typename T>
void DummyFunction(T o = T())
{
cout << T()() << endl;
}
#else
template <typename T>
void DummyFunction( )
{
T o;
cout << o() << endl;
}
#endif
void main(void)
{
DummyFunction< Dummy<first_t> >();
DummyFunction< Dummy<second_t> >();
} | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,662 network members.
|