Templates
-
Hi, Is it possible to use tests on the template typename(during compile, not in runtime)? Something like:
template<typename T> class MyClass{ ... T m_Var; ... void fun(){ #if T==double .... #else .. #endif } }
Cheers, Russellno, because T is assigned at runtime, while #if is evaluated before the compilation BTW, there's a mechanism called templates specializations, which allow you to design the templated class for a particuliar template parameter...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
Hi, Is it possible to use tests on the template typename(during compile, not in runtime)? Something like:
template<typename T> class MyClass{ ... T m_Var; ... void fun(){ #if T==double .... #else .. #endif } }
Cheers, Russelltemplate<typename T> class MyClass; template<> class MyClass<double> { ... T m_Var; ... void fun(){ // your double work } }; template<> class MyClass<int> { ... T m_Var; ... void fun() { // your int work } };
-- modified at 4:08 Thursday 20th July, 2006
-
no, because T is assigned at runtime, while #if is evaluated before the compilation BTW, there's a mechanism called templates specializations, which allow you to design the templated class for a particuliar template parameter...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
toxcct wrote:
T is assigned at runtime
I'm not sure, are you? templates are expanded during compilation. Elseway have you got a good link to learn something on "templates specializations". Thanks. Cheers, Russell
hum, ok, maybe not at runtime, but at compile time at least. unfortunately, preprocessing directives are executed BEFORE the compilation... so, no chance to do it like this
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
hum, ok, maybe not at runtime, but at compile time at least. unfortunately, preprocessing directives are executed BEFORE the compilation... so, no chance to do it like this
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
yes, I know that I can't use preprocessing directives, It was only an sintax example: I'm asking exacly on the template sintax that it can be used. Cheers, Russell
then search for "template specializations"
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
no, because T is assigned at runtime, while #if is evaluated before the compilation BTW, there's a mechanism called templates specializations, which allow you to design the templated class for a particuliar template parameter...
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
toxcct wrote:
because T is assigned at runtime
Templates ane not .NET generics. Compiler generates all specializations for used parameters.
template class CLS; CLS aaa; CLS > bbb;
At compile time, this will createdouble
andvector
specializations for template. -
toxcct wrote:
because T is assigned at runtime
Templates ane not .NET generics. Compiler generates all specializations for used parameters.
template class CLS; CLS aaa; CLS > bbb;
At compile time, this will createdouble
andvector
specializations for template.WTF :omg::wtf: what are you talking about ?
NrmMyth wrote:
Templates ane not .NET generics.
where have you seen me saying this ? and what is the point in saying this to me ? do you criticise my answer ? do you suggest something better than what i did ? :~ :suss:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
WTF :omg::wtf: what are you talking about ?
NrmMyth wrote:
Templates ane not .NET generics.
where have you seen me saying this ? and what is the point in saying this to me ? do you criticise my answer ? do you suggest something better than what i did ? :~ :suss:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
NrmMyth wrote:
I think that everyone has a right to correct you.
of course ! but not only he doesn't explain where i was wrong but he also "repeats" words that i never said ! :( where have you heard me saying that templates were .NET specifics ? :mad:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
NrmMyth wrote:
I think that everyone has a right to correct you.
of course ! but not only he doesn't explain where i was wrong but he also "repeats" words that i never said ! :( where have you heard me saying that templates were .NET specifics ? :mad:
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
-
Hi, Is it possible to use tests on the template typename(during compile, not in runtime)? Something like:
template<typename T> class MyClass{ ... T m_Var; ... void fun(){ #if T==double .... #else .. #endif } }
Cheers, Russellhttp://www.parashift.com/c++-faq-lite/templates.html#faq-35.7
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ