0

I was looking on the C++11 libraries, I'm not goot working with C++ objects and templates but I was trying to read the type of a variable and to run different code, but here there is an example that explains better:

if(is_scalar<typeid(list)>)
    cout << list << endl;
else
    for(...)
        cout << list[i] << endl;

I'm trying to do something similar, I think that function is_scalar is exactly what I need cause I need to split the vectors, arrays, and lists from ints, doubles, or floats, etc. It's hard to think of having the same variable that has different type in the same program but I'm trying to change it on runtime (still not working) or with pre-processor defines (that causes me the problem now).

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Umuril Lyerood
  • 175
  • 1
  • 1
  • 9
  • 2
    You will always know the type of a variable because you made the variable and you gave it a type. You cannot change it magically at runtime. You can infer it but your subsequent code has to be syntactically correct _at compile time_. Basically I'm not 100% sure what you're trying to do but I'm 90% sure you cannot do it in C++. Perhaps you are looking for PHP or JavaScript. – Lightness Races in Orbit Oct 25 '14 at 20:41
  • 6
    `typeid` is a runtime operator. You want `decltype`. I'm also unclear as to what you're asking exactly, though. – chris Oct 25 '14 at 20:42
  • 1
    Maybe you'd find the [pretty printer](http://stackoverflow.com/q/4850473/596781) useful? – Kerrek SB Oct 25 '14 at 20:46
  • To run different code based on the type of the object, you should look at [template specialization](http://www.cprogramming.com/tutorial/template_specialization.html). – Nard Oct 25 '14 at 20:48
  • I founded what i was looking for, decltype works fine for now and runs my code. Thanks chris! Now how can i close the question? – Umuril Lyerood Oct 25 '14 at 20:51
  • @UmurilLyerood you can post the details what you have done as answer, and mark that as accepted answer later. – wimh Oct 25 '14 at 21:26

2 Answers2

0

I solved my problem, I founded this code that I had to modify a bit

#include <iostream>
#include <vector>
using namespace std;

template< bool B > struct Algorithm {
    template<class T1> static int do_it (T1 & a) {
        for (int _n(((int)((a).size()))-1), i(0); i <= _n; i++)
            cout << *(next(a.begin(),i)) << endl;
        cout << endl; 
    }
};

template<> struct Algorithm<true> {
    template<class T1> static int do_it (T1 a)  { cout << a << endl; }
};

template<class T1>
int elaborate (T1 A)
{
    return Algorithm<std::is_scalar<T1>::value>::do_it( A ) ;
}

int main(){
    int a = 42;
    vector<int> b;
    b.push_back(1);
    b.push_back(2);
    elaborate(a);
    elaborate(b);
    return 0;
}

At the end, decltype didn't worked because the compiler tried to "read" code that never was "used".

Umuril Lyerood
  • 175
  • 1
  • 1
  • 9
0

Did you try to use decltype? This could help you.

http://en.cppreference.com/w/cpp/language/decltype

Gabriel
  • 3,564
  • 1
  • 27
  • 49