473,398 Members | 2,212 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,398 software developers and data experts.

explicit templates instantiation .... more


///// 1
Consider the _FUNCTION_ template

template <typename T>
int spaceOf ()
{
int bytes = sizeof T;
return bytes / 4
+ bytes % 4 > 0;
}
Since C++ templates requires more intelligence from the environment
'we' have to tell the compiler about the actualy type:
So now:
class C {};
typdef void (*ptrFun)(int);

int idx = spaceOf<C>();
int jdx = spaceOf<ptrFunc>();

Except how do i achieve the same feat here for this fuction template:

class D {
public:
template<typename T> // here we go again....
D( T& t, void(T::*f)() )
{}
};

class UseD {
void someSpecialFunc() {};
D *ptrD;
public:
UseD() {
ptrD = new (std::nothrow) D(*this, &UseD::someSpecialFunc);
// validate
}
};

////////////// 2
For starters this is a 'C' ism and I suspect (scanned the ISO C++
standard and couldn't find anything) both are legal (compiler didn't
complain)?

a. typedef _MYSTRUCT {} MYSTRUCT;
b. typedef MYSTRUCT {} MYSTRUCT;

b at first appeared 'troubling' to me when I first encounted this and -
admittidely - I thought the compiler would complain.

Jul 23 '05 #1
5 1734
ma******@pegasus.cc.ucf.edu wrote:
///// 1
Consider the _FUNCTION_ template

template <typename T>
int spaceOf ()
{
int bytes = sizeof T;
int bytes = sizeof(T);
return bytes / 4
+ bytes % 4 > 0;
}
Since C++ templates requires more intelligence from the environment
'we' have to tell the compiler about the actualy type:
So now:
class C {};
typdef void (*ptrFun)(int);

int idx = spaceOf<C>();
int jdx = spaceOf<ptrFunc>();

Except how do i achieve the same feat
What feat is that?
here for this fuction template:

class D {
public:
template<typename T> // here we go again....
D( T& t, void(T::*f)() )
{}
Are you looking to make use of 'T' here? Or of 't'? Or of 'f'? What
seems to be the problem?
};

class UseD {
void someSpecialFunc() {};
D *ptrD;
public:
UseD() {
ptrD = new (std::nothrow) D(*this, &UseD::someSpecialFunc);
// validate
}
};
Since I don't know what "feat" it is, I am not sure what to tell you.

////////////// 2
For starters this is a 'C' ism and I suspect (scanned the ISO C++
standard and couldn't find anything) both are legal (compiler didn't
complain)?
Is there a question here somewhere?
a. typedef _MYSTRUCT {} MYSTRUCT;
b. typedef MYSTRUCT {} MYSTRUCT;
Did you mean

typedef struct BLAH {} BLAHBLAH;

? Without the keyword 'struct', it's _illegal_.
b at first appeared 'troubling' to me when I first encounted this and -
admittidely - I thought the compiler would complain.


I feel your confusion.

V
Jul 23 '05 #2
> Except how do i achieve the same feat here for this fuction template:

class D {
public:
template<typename T> // here we go again....
D( T& t, void(T::*f)() )
{}

};

class UseD {
void someSpecialFunc() {};
D *ptrD;
public:
UseD() {
ptrD = new (std::nothrow) D(*this, &UseD::someSpecialFunc);
You can't. 14.8.1.4 in 1997 draft standard.
// validate
}

};

////////////// 2
For starters this is a 'C' ism and I suspect (scanned the ISO C++
standard and couldn't find anything) both are legal (compiler didn't
complain)?

a. typedef _MYSTRUCT {} MYSTRUCT;
b. typedef MYSTRUCT {} MYSTRUCT;
I think you mean

a. typedef struct _MYSTRUCT {} MYSTRUCT;
b. typedef struct MYSTRUCT {} MYSTRUCT;
b at first appeared 'troubling' to me when I first encounted this and -
admittidely - I thought the compiler would complain.


Struct names live in a different 'namespace', that's a relic from C.
Don't do that:

struct MYSTRUCT
{
};

is enough in C++.
Jonathan

Jul 23 '05 #3

Victor, how are you :)

|| Are you looking to make use of 'T' here? Or of 't'? Or of 'f'?
What seems to be the problem?

I have to explicitly initialize T. Couldn't quite figure out how to
get there 'in' this case. The template functoin spaceOf was trivial in
comparison.

|| ? Without the keyword 'struct', it's _illegal_.
The distiction between a and b lies in the underscore for the type
MYSTRUCT. I was perusing souce code and saw a mix of

typedef _ST1 {} ST1 ; // note the underscore for the type
typedef ST2 {} ST2; // not the same here.

I thought to myself WTH. It was - apparently - deliberate. One or
two instance and I'd assume 'typo' but given 12/13 structs with 6/7
with underscore and the rest withouth, I became curious. I just prefer
consistency but first I thought there was something I was missing

Jul 23 '05 #4
| I think you mean

| a. typedef struct _MYSTRUCT {} MYSTRUCT;
| b. typedef struct MYSTRUCT {} MYSTRUCT;

Yes, I'm so sorry... Crud I can't belive I missed the keyword. I was
so hung up on the underscore that I mistyped.

Jul 23 '05 #5
ma******@pegasus.cc.ucf.edu wrote:
Victor, how are you :)
Thanks, I am fine. How are you?
Are you looking to make use of 'T' here? Or of 't'? Or of 'f'?
What seems to be the problem?


I have to explicitly initialize T.


I don't understand this statement. In a templated constructor you
cannot use explicit template argument syntax, you _have_to_ rely on
the template argument deduction from the function argument:

#include <typeinfo>
#include <iostream>
struct Foo {
template<class T> Foo(T t) {
std::cout << "Foo<" << typeid(T).name() << ">(" << t << ")\n";
}
};

int main() {
Foo foo(42);
}

(compile and run it, you should see something like)
---------
Foo<int>(42)
---------
Couldn't quite figure out how to
get there 'in' this case. The template functoin spaceOf was trivial
in comparison.


I don't understand why you'd need to explicitly specify the template
argument when it can be easily deduced by the compiler.

V
Jul 23 '05 #6

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

Similar topics

4
by: C. Carbonera | last post by:
/* 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...
1
by: Leslaw Bieniasz | last post by:
Cracow, 15.09.2004 Hi, I am writing a big C++ project using BCB 4.0. The project consists of several dlls and exes. Each of the dll is composed of several units (that is cpp files with...
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...
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>
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
4
by: yuanhp_china | last post by:
I define a class in A.h: template <class Tclass A{ public: void get_elem( const T&) ; };
1
by: Rahul | last post by:
Hi Everyone, I was looking at the link, http://www.comeaucomputing.com/4.0/docs/userman/export.html and it looks like the basic purpose of exporting templates is make sure that they are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...

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.