🔒 Locked
Template troubles
Advertisement
Original Post
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]
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]
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 ]
This compiles fine on VC++.net
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]
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]
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:
and use like so:
Right now I settled for
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.
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.
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
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 ]
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.
Advertisement