roverm
asked on
class def
I receive an error when I try this:
template <typename HANDLE _Init=NULL>
class CMyClass
{
public:
CMyClass()
{
m_hHanlde = _Init;
}
protected:
HANDLE m_hHandle;
};
Now to define a specility of the class:
typedef CMyClass<-1> CMyOtherClass;
This is where the compiler gives an error: a signed int cannot be converted/casted to a void* (which is a HANDLE)
typedef CMyClass<0xFFFFFFFF> CMyOtherClass;
Doesn't work either !
Anybody knows the answer ?
grtx, RoverM
template <typename HANDLE _Init=NULL>
class CMyClass
{
public:
CMyClass()
{
m_hHanlde = _Init;
}
protected:
HANDLE m_hHandle;
};
Now to define a specility of the class:
typedef CMyClass<-1> CMyOtherClass;
This is where the compiler gives an error: a signed int cannot be converted/casted to a void* (which is a HANDLE)
typedef CMyClass<0xFFFFFFFF> CMyOtherClass;
Doesn't work either !
Anybody knows the answer ?
grtx, RoverM
More precisely (from '96 Draft):
========================== ========== ======
3 A non-type template-parameter shall have one of the following (option-
ally cv-qualified) types:
--integral type, accepting an integral constant expression as an argu-
ment,
--enumeration type, accepting an integral constant expression as an
argument,
--pointer to object, accepting an address constant expression desig-
nating a named object with external linkage,
--reference to object, accepting an lvalue expression designating a
named object with external linkage,
--pointer to function, accepting an expression of type pointer to
function designating a function with external linkage,
--reference to function, accepting an lvalue expression designating a
function with external linkage,
--pointer to member, accepting an address constant expression desig-
nating a named member of a class.
========================== ========== ======
==========================
3 A non-type template-parameter shall have one of the following (option-
ally cv-qualified) types:
--integral type, accepting an integral constant expression as an argu-
ment,
--enumeration type, accepting an integral constant expression as an
argument,
--pointer to object, accepting an address constant expression desig-
nating a named object with external linkage,
--reference to object, accepting an lvalue expression designating a
named object with external linkage,
--pointer to function, accepting an expression of type pointer to
function designating a function with external linkage,
--reference to function, accepting an lvalue expression designating a
function with external linkage,
--pointer to member, accepting an address constant expression desig-
nating a named member of a class.
==========================
kijk ook eens naar URL:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lounge&qid=10111117
- or -
https://www.experts-exchange.com/Q.10111117-2392612 (non-cookie login)
https://www.experts-exchange.com/jsp/qShow.jsp?ta=lounge&qid=10111117
- or -
https://www.experts-exchange.com/Q.10111117-2392612 (non-cookie login)
Kangaroo, the parameter that roverm uses IS a constant expression, of course it must be a parameter that is known during compile-time, but that is the case in his example. So that is not the source of the problem.
Try to cast it explicitly to HANDLE (void*) this will solve you're problem.
Luc
Try to cast it explicitly to HANDLE (void*) this will solve you're problem.
Luc
HANDLE is not any of the allowed types (see above). A HANDLE is typedef-ed as a void*, so only void* (HANDLE's) with external linkage can be used as parameter for the template.
Explicit casting to HANDLE will not solve the problem.
Explicit casting to HANDLE will not solve the problem.
>> HANDLE is not any of the allowed types
That is, it is allowed if it has external linkage, as in my example.
That is, it is allowed if it has external linkage, as in my example.
ASKER
Sorry, still get errorsssssss!
like 'invalid template error'
like 'invalid template error'
ASKER
Further, I'm trying to instanciate a new definition using TypeDef, NOT by using instanciating a class !
RoverM
RoverM
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>> I'm trying to instanciate a new definition using TypeDef
You can not instantiate a tmplate specialization with a typedef, if that is what you are trying to do.
You can not instantiate a tmplate specialization with a typedef, if that is what you are trying to do.
ASKER
Kangaroo:
You're right! It cannot be done!
So I'll give you the points!
D'Mzzl!
RoverM
You're right! It cannot be done!
So I'll give you the points!
D'Mzzl!
RoverM
You would need a declaration:
extern HANDLE MyHandle;
a definition
HANDLE MyHandle = -1;
Parameters for your template must always heave external linkage:
extern HANDLE hGl;
void f()
{
HANDLE hLc;
CMyClass<hLc> cm1; // error
CMyClass<hGl> cm2; // ok
//....
}