Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > explicit templates instantiation .... more

Reply
Thread Tools

explicit templates instantiation .... more

 
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      07-13-2005

///// 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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-13-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Jonathan Mcdougall
Guest
Posts: n/a
 
      07-13-2005
> 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

 
Reply With Quote
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      07-13-2005

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

 
Reply With Quote
 
ma740988@pegasus.cc.ucf.edu
Guest
Posts: n/a
 
      07-14-2005
| 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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-14-2005
(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Explicit instantiation of templates: any experience? Steven T. Hatton C++ 3 12-05-2006 03:15 AM
Explicit template instantiation from template function doesn't compile? Fernando Cuenca C++ 4 09-06-2004 04:06 PM
problem with explicit template instantiation in Visual C++ 6.0 . C. Carbonera C++ 4 02-05-2004 05:05 PM
Re: std::map explicit instantiation Rolf Magnus C++ 0 07-23-2003 02:07 PM



Advertisments