Jump to content
  • Advertisement

Archived

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

IndirectX

Template troubles

This topic is 5856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!