Thread: knowing the type of the array/vector at compile time with type_traits

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    knowing the type of the array/vector at compile time with type_traits

    this question regards a template function , with some class T as a paramter type.
    we know that we can get whether the type is some T[] at compile time with type_traits' std::is_array. my question is : can we get the type T itself at compile time and , e.g. hold it as a string or print it ?

    and another one , can we detect , at compile time if T is an instance of std::vector and can we get the inner type T as above?

    thanks in advance.

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think this might be useful : std::is_same - cppreference.com

  3. #3
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    it's useful if you have a set of types to compare with , not if you want a generic function..

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You can't compare a template to a set type?

    Code:
    template<typename T>
    void function(T arg)
    {
        if (std::is_same<T, char*>::value)
        {
            /* ... */
        }
    }
    Does this not compile?
    Last edited by MutantJohn; 04-05-2015 at 10:49 AM.

  5. #5
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    it will , but the point I is that I need to print anything that may come - I need to know what is the underline type of the array/vector for any T , not just char/int ... rest of known types (to me) but any type.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want to do this? Maybe there are alternative approaches.

    One thing that comes to mind is that you could try the RTTI functionality of typeid(x).name() for some object x, but that has its limitations in that the name is not guaranteed to be unique or even "human readable", and it does not satisfy your compile time requirement.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    well , I'm building an automation platform and I want to be able to prompt the developer about the variable status and change.
    basically , I want to write an all-encompassing "toString" function , with similar behavior like javascript.
    I am using visual studio 2013 if it helps anyway.
    Code:
    the logic goes like this:
    if the variable is a const char * / const wchar_t * / std :: string / std :: wstring - turn the string to wstring and print it
    else if the variable is a char / int / long etc - print it as it
    if the variable is a pointer - type it as T* <memory address> where T is the type name 
    
    now it gets trickier :
    if the variable is a one-dimensional array of T or std::vector<T> and T is one of the types above - print it as [ array[0],array[1].. ] etc. - where array[i] is the value of the above
    if the variable is object - just print it as "{object}"
    if the variable is array of objects - print it as "[{obj}..{obj}]"
    all the rest are pretty trivial with all sorts of type_traits functions (like is_pointer , is_object etc). but the vector/array thing is kinda tricky..

    I know it may seems caprice at first , but this function is important for the completeness of the platform


    now that I see it again , I can use std::is_same. so my question is purely theoretical now.
    yes , I have thought about analyzing typeif :: name , I'll see if VC++ gives some human-readable strings for that

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you want to specialize a function for certain types, use overloading. E.g.:

    void foo(std::vector<T>)
    void foo(int)
    // etc

    If you want function to handle a set of specific types, then use overloading and type dispatch. Basically, you use type traits to create std::false/true types, then you create overloads for true/false types and call them:

    Code:
    void foo()
    {
        bar(std::is_integral_t<T>());
    }
    
    void bar(std::true_type)
    void bar(std::false_type)
    If you just want to print out a type's name, use laserlight's suggestion of typeid.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-12-2013, 10:11 AM
  2. 2D array, size unknown at compile time
    By gah_ribaldi in forum C# Programming
    Replies: 10
    Last Post: 12-04-2009, 05:42 PM
  3. knowing the amount of array being occupied
    By winsonlee in forum C Programming
    Replies: 1
    Last Post: 03-17-2004, 06:07 AM
  4. Replies: 0
    Last Post: 11-29-2002, 10:24 PM