By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,118 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,118 IT Pros & Developers. It's quick & easy.

explicit templates instantiation .... more

P: n/a

///// 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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
> 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

P: n/a

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

P: n/a
| 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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.