Skip to main content
gamedev.net
🔒 Locked

Template troubles

Started by IndirectX Jun 8, 2002 at 5:56 PM 8 replies 1.6k views
Original Post
IndirectX
IndirectX
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]
---visit #directxdev on afternet <- not just for directx, despite the name
the Speed Bump
the Speed Bump

    
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]
"There is only one everything"
Fruny
Fruny
quote:
Original post by the Speed Bump
templace



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

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

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
IndirectX
IndirectX
quote:
Original post by Fruny
only integral types.



Any particular reason why?
---visit #directxdev on afternet <- not just for directx, despite the name
Cedric
Cedric
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]
IndirectX
IndirectX
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.
---visit #directxdev on afternet <- not just for directx, despite the name
Fruny
Fruny
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
IndirectX
IndirectX
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.
---visit #directxdev on afternet <- not just for directx, despite the name
fallenang3l
fallenang3l
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]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.