Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 928169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:58:02+00:00 2026-05-15T19:58:02+00:00

I have a underlying API that passes a const char* and a length: foo(const

  • 0

I have a underlying API that passes a const char* and a length:

foo(const char* data, const uint32_t len);

I’d like to wrap this data/length in a light weight container that can be iterated and has the ability to be randomly accessed but not make a copy (e.g. like a vector). What is the best way to achieve this? The const char* data is not necessarily a ‘string’; it may contain NULL’s throughout.

I am using STL and Boost. I’ve seen boost::as_array<> and as_literal<> — is one of these appropriate here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T19:58:02+00:00Added an answer on May 15, 2026 at 7:58 pm

    It would be easy to make such a class. Something like this:

    template <class T>
    class array_ref {
    public:
        // makes it work with iterator traits..
        typedef T         value_type;
        typedef size_t    size_type;
        typedef ptrdiff_t difference_type;
    
        typedef T*        pointer;
        typedef T*        iterator;
        typedef T&        reference;
    
        typedef const T*  const_pointer;
        typedef const T*  const_iterator;
        typedef const T&  const_reference;
    
    public:
        array_ref(T *p, size_t n) : data_(p), len_(n) {}
    
        // iteration
        iterator begin()             { return data_; }
        iterator end()               { return data_ + len_; }
        const_iterator begin() const { return data_; }
        const_iterator end() const   { return data_ + len_; }
    
        // access
        reference operator[](size_t n)             { return data_[n]; }
        reference at(size_t n)                     { return data_[n]; }
        const_reference operator[](size_t n) const { return data_[n]; }
        const_reference at(size_t n) const         { return data_[n]; }
    
        // capacity
        size_t size() const { return len_; }
        bool empty() const  { return size() == 0; }
    
        // raw access
        T* data() const { return data_; }
    
        // etc...
    private:
        T* data_;
        size_t len_;
    };
    

    This looks like a bunch of code, most of it isn’t strictly necessary. However, since it is a template the compiler will only generate code for the methods used. And the actual class itself only uses space for the pointer and the length members.

    But in the end, this really isn’t much of a gain. Since pointers themselves are nice pointers, I’d probably just use raw pointers here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have class like this (simplified): class Foo_p; class Foo { private: Foo_p
I have this problem, that usually api entry point works fine: https://www.google.com/analytics/feeds/accounts/default but when
We have an underlying .h file in our platform code that needs built differently
I have a webpage that I don't have the ability to change the underlying
I have this application where I implement the ActionBar Fragment interface. Underlying the interface,
Say you have an API that is not accessible to change: List<LegacyObject> getImportantThingFromDatabase(Criteria c);
I have a service implemented in MVC4 / ASP.NET Web Api. I would like
I have inherited a piece of Javascript that uses the Google Feed API. It
I have a thin (SOAP) API that only knows how to set and get
I have a function that returns an NSError object by reference: NSData *foo(NSData *foo,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.