explicit templates instantiation .... more

M

ma740988

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

Victor Bazarov

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

Jonathan Mcdougall

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
 
M

ma740988

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
 
M

ma740988

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

Victor Bazarov

Victor, how are you :)

Thanks, I am fine. How are you?
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)
---------
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top