473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 > > ( );
}
Jul 22 '05 #1
4 2578
C. Carbonera wrote:
/* [snip]
Could someone please test the code given below in Visual C++.NET
and let me know if it works?
It work in VC++ 2003 with a few minor modifications to include the correct
header:

Thanks,
CCarbonera
*/
#include <fstream.h>
#include <fstream>
using namespace std;

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 > > ( );
}


Jul 22 '05 #2
[snip]

#include <fstream>
Sorry, this should be <iostream> , not <fstream> .
using namespace std;

[snip
Jul 22 '05 #3

"C. Carbonera" <cc********@msn.com> wrote in message
news:df**************************@posting.google.c om...
/*
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 > > ( );
}


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
Jul 22 '05 #4
"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message news:<bv************@ID-216073.news.uni-berlin.de>...
"C. Carbonera" <cc********@msn.com> wrote in message
news:df**************************@posting.google.c om...
/*
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 > > ( );
}


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


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> >();
}
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dmitry Epstein | last post by:
Here is an example of a problem, which I tried to reduce to its bare essentials: // begin test1.cpp class E { public: template<class T> static void f(); }; template<class T> void E::f() {}
4
by: CoolPint | last post by:
I would be grateful if someone could point out if I am understanding correctly and suggest ways to improve. Sorry for the long message and I hope you will kindly bear with it. I have to make it...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
5
by: Andy | last post by:
Hi, I got a little confused on 'instantiation' and 'specialization', espcially for explicit instantiation and explicit sepcialization. Can anybody explain the difference? Thanks a lot! ...
3
by: Dilip | last post by:
I am stumbling my way through C++ templates and I keep running into way too many questions. Here is one: If a library writer exposes a function template like so: template<typename T> void...
3
by: Steven T. Hatton | last post by:
Has anybody here used explicit instantiation of templates? Has it worked well? Are there any issues to be aware of? -- NOUN:1. Money or property bequeathed to another by will. 2. Something...
1
by: krunalbauskar | last post by:
Hi, Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. For example - <snippet> #include <iostream> #include <vector>
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
0
by: greek_bill | last post by:
Hi, I have a template function for which I use SFINAE to restrict one of the parameters. Then I also have a partial specialization of this function.I would like to provide an explicit...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.