Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost TraitsType Question
From: Marshall Clow (mclow.lists_at_[hidden])
Date: 2012-10-04 14:15:16


On Oct 4, 2012, at 10:58 AM, "Rawate, Vishal CIV NAVAIR-WD-5-2-7-1-1-0-E., 527110E" <vishal.rawate_at_[hidden]> 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;
> }
> }

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_typetraits/reference/is_same.html

-- Marshall

Marshall Clow Idio Software <mailto:mclow.lists_at_[hidden]>

A.D. 1517: Martin Luther nails his 95 Theses to the church door and is promptly moderated down to (-1, Flamebait).
        -- Yu Suzuki


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net