Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
Something like this:

C++
template <struct T>
class Foo {

};


The class is going to load binary file and cast it onto given structure.
I found template <class T> compiling. Is this proper way?

What I have tried:

The code above, and searching google as well. Everywhere struct type was actually defined, not as I wish.
Posted
Updated 21-May-16 23:59pm
v2
Comments
Sergey Alexandrovich Kryukov 21-May-16 23:17pm    
Not clear. Templates are designed to avoid type cast...
—SA
[no name] 21-May-16 23:36pm    
SA has made the important point. Perhaps you need to understand templates better. The template instantiation must know the typename T (which can be a structure) at compile time. Where does typecasting come into it? The question is vague and does not have proper meaning to those who may know the answer.
Cansisti 22-May-16 5:59am    
Improved question, thanks
Richard MacCutchan 22-May-16 7:37am    
You need to explain exactly what you want this template for. You also need to understand that templates are not used in place of casting, they are used to generate different versions of the basic code. It may well be that the solution to your problem has nothing to do with templates.
[no name] 22-May-16 9:01am    
You say "The class is going to load binary file and cast it onto given structure". Sure a class can do this - now where does the template come in?

1 solution

T is just a placeholder for the real type that will be used wherever class foo appears later in your source code. see Templates (C++)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900