Archived

This topic is now archived and is closed to further replies.

IndirectX

Template troubles

Recommended Posts

IndirectX    122
Consider this templated class: template <class X, X Y> struct S { }; Why is it so that I can define a: S <int, 50> a; and not b: S <PVOID, (PVOID)-1> b; This gives me error C2964: invalid expression as template parameter What's wrong with PVOID that is right with int? [*] [*] Besides PVOID not being an integral type, but why does it matter? [edited by - IndirectX on June 8, 2002 7:05:09 PM]

Share this post


Link to post
Share on other sites
the Speed Bump    240

    
templace <typename X,int Y,float Z>
struct Blegh
{
};

Blegh<int,50,3.141596> boojum;


This what you mean?

I'm hip because I say "M$" instead of "MS".

[edited by - the Speed Bump on June 9, 2002 12:15:24 PM]

Share this post


Link to post
Share on other sites
Cedric    158
This compiles fine on VC++.net
template< float y >
class X{};
int main(){
X<5.0f> x;
}

Why couldn't you do it?

The OP's code works on my compiler (VC++.net), when I #include windows.h. If you doubt your compiler, you can always try Comeau's web-based compiler (www.comeaucomputing.com). It's pretty much the most Standard-compliant compiler around.

Cédric

[edited by - cedricl on June 9, 2002 5:53:22 PM]

Share this post


Link to post
Share on other sites
IndirectX    122
Floats compile fine, but PVOID and HANDLE don''t. I need this functionality for my smart handle class, which I would like to declare like so:

  
template <class H, class C, H InvalidHandleValue>
class CHandle;
// H - wrapped handle type

// C - cleaner type, has a "static void Clean(H handle);"


and use like so:

  
CHandle <HANDLE, CloseHandleCleaner, NULL> hFile = CreateFile(...);
CHandle <HANDLE, FindCloseCleaner, INVALID_HANDLE_VALUE> hFind = FindFirst(...);

Right now I settled for

  
template <class H, class C, class INV = InvalidHandleValue <H> >

where INV is another class that has "static H GetValue();" function to return the invalid handle value. I was wondering why the first method couldn''t be implemented. InvalidHandleValue is a default that returns 0.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:

Why is it so that I can define a:

S <int, 50> a;

and not b:

S <PVOID, (PVOID)-1> b;

This gives me

error C2964: invalid expression as template parameter



If PVOID is a type, how can you subtract one from it?

Share this post


Link to post
Share on other sites
Fruny    1658
quote:
Original post by Anonymous Poster
If PVOID is a type, how can you subtract one from it?



That''s called a cast.

IndirectX : try static_cast(-1)

Cédric : The fact that something compiles in VC doesn''t mean it is correct. The Standard says you cannot.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites
IndirectX    122
quote:
Original post by Fruny
IndirectX : try static_cast<void*>(-1)


static_cast won''t compile, and reinterpret_cast gives me the same error.
quote:

The Standard says you cannot.


Damn standards. I guess I''ll just have to live with it. Thanks anyway.

Share this post


Link to post
Share on other sites
fallenang3l    144
quote:
Original post by Fruny

You can't have a float template typed parameter, only integral types.

And no, pointers are not integral types, they're pointers.




An expression argument can be an integral type, and enumeration type, a reference or a pointer.

[edited by - fallenang3l on June 9, 2002 7:24:16 PM]

Share this post


Link to post
Share on other sites