/*
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 DummyFunctionDo esNotWork(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 DummyFunctionWo rks(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 DummyFunctionWo rks(T &o)
{ cout << o() << endl; }
template < typename T >
void DummyFunctionDo esNotWork()
{ 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;
DummyFunctionWo rks < Dummy< first_t > > ( frst );
DummyFunctionWo rks < Dummy< second_t > > ( scnd );
cout<< "The following produces the wrong output:" << endl;
DummyFunctionDo esNotWork < Dummy < first_t > > ( );
DummyFunctionDo esNotWork < Dummy < second_t > > ( );
} 4 2609
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 DummyFunctionWo rks(T &o) { cout << o() << endl; }
template < typename T > void DummyFunctionDo esNotWork() { 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; DummyFunctionWo rks < Dummy< first_t > > ( frst ); DummyFunctionWo rks < Dummy< second_t > > ( scnd );
cout<< "The following produces the wrong output:" << endl; DummyFunctionDo esNotWork < Dummy < first_t > > ( ); DummyFunctionDo esNotWork < Dummy < second_t > > ( ); }
[snip] #include <fstream>
Sorry, this should be <iostream> , not <fstream> .
using namespace std;
[snip
"C. Carbonera" <cc********@msn .com> wrote in message
news:df******** *************** ***@posting.goo gle.com... /* 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 DummyFunctionDo esNotWork(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 DummyFunctionWo rks(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 DummyFunctionWo rks(T &o) { cout << o() << endl; }
template < typename T > void DummyFunctionDo esNotWork() { 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; DummyFunctionWo rks < Dummy< first_t > > ( frst ); DummyFunctionWo rks < Dummy< second_t > > ( scnd );
cout<< "The following produces the wrong output:" << endl; DummyFunctionDo esNotWork < Dummy < first_t > > ( ); DummyFunctionDo esNotWork < 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 Turkanis" <te******@kanga roologic.com> wrote in message news:<bv******* *****@ID-216073.news.uni-berlin.de>... "C. Carbonera" <cc********@msn .com> wrote in message news:df******** *************** ***@posting.goo gle.com... /* 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 DummyFunctionDo esNotWork(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 DummyFunctionWo rks(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 DummyFunctionWo rks(T &o) { cout << o() << endl; }
template < typename T > void DummyFunctionDo esNotWork() { 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; DummyFunctionWo rks < Dummy< first_t > > ( frst ); DummyFunctionWo rks < Dummy< second_t > > ( scnd );
cout<< "The following produces the wrong output:" << endl; DummyFunctionDo esNotWork < Dummy < first_t > > ( ); DummyFunctionDo esNotWork < 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 DummyFunctionDo esNotWork < Dummy < first_t > > ( ) and
void DummyFunctionDo esNotWork < 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()(voi d) { return "First
Function"; }
template<>
const char* Dummy<second_t> ::operator()(vo id) { 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> >();
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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() {}
|
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 elaborate to
make sure my questions are clear enough.
Let's say I need to write a function whose logic is same for all types
(T) except in the case of T * (including const T *). Furtheremore ,
the function needs to be written differently for...
|
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, MathFuncPtr> predefinedFunctions;
// lots of other stuff
void makeDictionary(){ predefinedFunctions=&F::f_sin(); }
};
|
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!
Andy
|
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 some_func(T const& x)
{
}
| |
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 handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
|
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>
|
by: yuanhp_china |
last post by:
I define a class in A.h:
template <class Tclass A{
public:
void get_elem( const T&) ;
};
|
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 instantiation of the
partially specialized version, but my compiler (VC8) complains because
it fails the SFINAE version.
I just realized as I was typing this that I'm using partial
_function_ specialization. I'm sure I remember reading somewhere that
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |