Declare a Const array

This is a discussion on Declare a Const array within the C++ Programming forums, part of the General Programming Boards category; Why is it better? AFAICT, you're doing that because you want the very minor syntax difference, but it requires the ...

  1. #16
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Why is it better? AFAICT, you're doing that because you want the very minor syntax difference, but it requires the use of templates. The vector constructor I used is an iterator constructor. It's typical code to use arithmetic in order to generate iterators.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by whiteflags View Post
    Why is it better? AFAICT, you're doing that because you want the very minor syntax difference...
    No, it saves you from manually inputting the size.
    Less manual size inputting = fewer bugs.
    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.

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    You know the size parameter doesn't have to be "manual" in the sense that there is a mutable variable or literal constant lying around.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I am perfectly aware that there are "safe" ways to do things, but better safe than sorry.
    The time comes when you will do modifications to your code, and your size parameter suddenly becomes a hidden runtime bug. Not fun.
    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.

  5. #20
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Yeah, I do agree. The time to change code is not the time to take a mental vacation, either so that would be my advice to newfriends.

  6. #21
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Just wanted to throw this out there. I'd even prefer something like this in the single context this trick actually works.
    Code:
    template <typename T, std::size_t retval>
    inline std::size_t SizeOf ( T (&foo)[retval] )
    {
       return retval;
    }
    
    Thing thingconstructed(something, SizeOf(something));
    Thing has a constructor with the dreaded size parameter of course.

    Even more problematic: Ignoring the bad function name, in C++11 constexpr might work out better than inline anyway, but constexpr has rules, and this isn't important, so I can't be bothered. Applying the trick to every possible instance just grinds my gears.
    Last edited by whiteflags; 01-03-2012 at 08:55 PM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of const pointers to const struct
    By albundy in forum C Programming
    Replies: 4
    Last Post: 08-24-2011, 09:16 PM
  2. How do you declare an array in a class?
    By casablanca in forum C++ Programming
    Replies: 9
    Last Post: 08-03-2011, 12:16 PM
  3. How to declare const data member in class
    By chottachatri in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2008, 04:54 AM
  4. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 05:12 PM
  5. How to declare this array?
    By ooosawaddee3 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2002, 01:51 PM

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