Using typeid to check if something is an array

This is a discussion on Using typeid to check if something is an array within the C++ Programming forums, part of the General Programming Boards category; http://cboard.cprogramming.com/showt...threadid=38815 That topic is related, sort of... its the same class, but this is a different thing I'm trying to ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    "cannot specify explicit initializer for arrays"

    "Any" type using "Boost"

    That topic is related, sort of... its the same class, but this is a different thing I'm trying to do.

    I have code that technically would "work"...

    The error is:
    cannot specify explicit initializer for arrays

    If the user tells it it is a char* instead of just a regular array, it will compile, only it isn't a character array anymore really... and I want the user to be able to choose between char[] and char*...

    Code:
    holder(const ValueType & value)
                  : held(value)
                {//non array (or pointer)
                }
    That won't take a string... something like

    Code:
    char x[10] = "abcd";
    holder y(x);
    won't compile, while

    Code:
    char x[10] = "abcd";
    holder y((char*)x);
    or

    Code:
    char *x = "abcd";
    holder y(x);
    will.

    My understanding of the problem is that the fact that it is declared as

    ValueType held;

    in the class, and it can't figure out how to make something like

    "char[20] held;"
    Last edited by Trauts; 04-30-2003 at 06:58 PM.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    162
    did u try &x ?

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    That wouldn't work if you did

    Code:
    holder y("abcd");
    Also, how can you specify a template type with that?

    There's this function in the main class called "any"

    Code:
        template<typename ValueType>
        ValueType any_cast(const any & operand)
        {
            const ValueType * result = any_cast<ValueType>(&operand);
            if(!result)
                throw bad_any_cast();
            return *result;
        }
    And if you pass it a character array when you initialized the holder (it does it when you declare something like
    char y[10] = "abcd";
    any x = &y;
    That works... but you can't get it back because it can't convert 'const char [10]' to 'char *'


    Er... I just found a way... but I don't know how to make it more convenient:

    cout << *any_cast<char(*)[10]>(x);

    Not only that, you need to know the exact size... and there's no way to do that.
    Last edited by Trauts; 04-30-2003 at 07:56 PM.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Unless someone can figure it out, I'll just remove the code that let you do the character array in the first place.

    Thanks anyway!
    Last edited by Trauts; 04-30-2003 at 08:01 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Array to Check Equality
    By dcwang3 in forum C Programming
    Replies: 7
    Last Post: 10-03-2008, 12:56 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21