Hello, I am writing a function using templates in C++.I want to find the type of variable that is being to the function. I was wondering if I could use the Boost TypeTraits library or is there another library that I can use. I would highly appreciate any help. Thanking in advance. Vishal CodeSnipet: Template<typename T> Void setTag(T myVariable) { If(T == short) { Do something; } If(T == long) { Do something; } }
On Thu, 4 Oct 2012, Rawate, Vishal CIV NAVAIR-WD-5-2-7-1-1-0-E., 527110E wrote:
Hello,
I am writing a function using templates in C++.I want to find the type of variable that is being to the function. I was wondering if I could use the Boost TypeTraits library or is there another library that I can use. I would highly appreciate any help.
Thanking in advance.
Vishal
CodeSnipet:
Template<typename T>
Void setTag(T myVariable) { If(T == short) { Do something; }
If(T == long) { Do something; }
}
Wouldn't standard overloading (or function template complete specialization) work just as well here? template <typename T> void setTag(T myVariable) { /* Base case */ } void setTag(int myVariable) { /* int case */ } void setTag(long myVariable) { /* long case */ } -- Jeremiah Willcock
Yes Function overloading or Template specialization would work. Bu again all the functions have to perform the same tasks just the types are different. For example I would like to do this. Template<typename T> Void setTag(T myVariable) { If(T == short) { myType = short; } If(T == long) { myType = long; } } Thanks for all the help. -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Jeremiah Willcock Sent: Thursday, October 04, 2012 11:15 To: boost-users@lists.boost.org Subject: Re: [Boost-users] Boost TraitsType Question On Thu, 4 Oct 2012, Rawate, Vishal CIV NAVAIR-WD-5-2-7-1-1-0-E., 527110E wrote:
Hello,
I am writing a function using templates in C++.I want to find the type of variable that is being to the function. I was wondering if I could use the Boost TypeTraits library or is there another library that I can use. I would highly appreciate any help.
Thanking in advance.
Vishal
CodeSnipet:
Template<typename T>
Void setTag(T myVariable) { If(T == short) { Do something; }
If(T == long) { Do something; }
}
Wouldn't standard overloading (or function template complete specialization) work just as well here? template <typename T> void setTag(T myVariable) { /* Base case */ } void setTag(int myVariable) { /* int case */ } void setTag(long myVariable) { /* long case */ } -- Jeremiah Willcock _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Oct 4, 2012, at 3:19 PM, "Rawate, Vishal CIV NAVAIR-WD-5-2-7-1-1-0-E., 527110E"
Yes Function overloading or Template specialization would work. Bu again all the functions have to perform the same tasks just the types are different.
For example I would like to do this. Template<typename T>
Void setTag(T myVariable) { If(T == short) { myType = short; }
If(T == long) { myType = long; } }
I'm afraid I don't get what you're doing. Maybe you need to show a more concrete example. Is "MyType" a type? If so, why not just say: typedef T MyType; and be done with it? -- Marshall Marshall Clow Idio Software mailto:mclow.lists@gmail.com A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki
On Oct 4, 2012, at 10:58 AM, "Rawate, Vishal CIV NAVAIR-WD-5-2-7-1-1-0-E., 527110E"
Hello,
I am writing a function using templates in C++.I want to find the type of variable that is being to the function. I was wondering if I could use the Boost TypeTraits library or is there another library that I can use. I would highly appreciate any help.
Thanking in advance.
Vishal
CodeSnipet:
Template<typename T>
Void setTag(T myVariable) { If(T == short) { Do something; }
If(T == long) { Do something; } }
The way I usually do this is via template specialization: #include <iostream> template <typename T> void foo ( T val ) { std::cout << "other\n"; } template <> void foo (int val) { std::cout << "int\n"; } template <> void foo (long val) { std::cout << "long\n"; } int main ( int argc, char *argv [] ) { foo ( 1 ); foo ( 1L ); foo ( "abc" ); } but if you really want to use type traits, I think that boost::is_same is what you want. http://www.boost.org/doc/libs/1_51_0/libs/type_traits/doc/html/boost_typetra... -- Marshall Marshall Clow Idio Software mailto:mclow.lists@gmail.com A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait). -- Yu Suzuki
Hi, why don't you use overloading for this? void setTag(short myVariable){ //do something } void setTag(long myVariable){ //do something } On 2012-10-04 19:58, Rawate, Vishal CIV NAVAIR-WD-5-2-7-1-1-0-E., 527110E wrote:
Hello,
I am writing a function using templates in C++.I want to find the type of variable that is being to the function. I was wondering if I could use the Boost TypeTraits library or is there another library that I can use. I would highly appreciate any help.
Thanking in advance.
Vishal
CodeSnipet:
Template<typename T>
Void setTag(T myVariable) { If(T == short) { Do something; }
If(T == long) { Do something; }
}
participants (4)
-
Jeremiah Willcock
-
Marshall Clow
-
Oswin Krause
-
Rawate, Vishal CIV NAVAIR-WD-5-2-7-1-1-0-E., 527110E