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 8699029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:51:03+00:00 2026-06-13T01:51:03+00:00

I have some functions that I use to convert a 2D variant SAFEARRAY into

  • 0

I have some functions that I use to convert a 2D variant SAFEARRAY into various STL containers, kinda like so (illustrative only)

template<typename T>
std::set<T> SetFromSafeArray(VARIANT srcArray)
{
    CComSafeArray<T> srcComArray(srcArray.parray);
    std::set<T> destContainer;

    for (ULONG i=0;i<srcComArray.GetCount();++i)
        destContainer.insert(srcComArray.GetAt(i));

    return destContainer;
}

I feel it’s not a very c++-ish way of going about it and it means there’s a separate function for each STL container I convert to.

My idea was to write a wrapper and custom iterator for CComSafeArrays so I could just do…

std::copy(srcComArray.begin(), srcComArray.end(), destContainer.begin());

but having never written an iterator before and being a novice I really don’t know if it will be easy.

Is a custom CComSafeArray iterator my best, standard c++ like, option (in which case I’m sure I can find a good tutorial on writing an iterator)? Or is there some other way of going about it?

Boost is not an option.

TIA

  • 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-06-13T01:51:04+00:00Added an answer on June 13, 2026 at 1:51 am

    My idea was to write a wrapper and custom iterator for CComSafeArrays

    This is very good idea for creating iterator, but you don’t need a wrapper around CComSafeArray<T>, only iterator is needed.

    so I could just do…

    std::copy(srcComArray.begin(), srcComArray.end(), destContainer.begin());
    

    But instead of doing your way, you can do this:

    SomeContainer<T> destContainer(begin(srcComArray), end(srcComArray));   
    

    Because almost every STL container has constructor from range (pair of iterators).

    Assuming you have written iterator over CComSafeArray<T> – functions begin/end will be like these:

    template <typename T>
    CComSafeArrayIterator<T> begin(CComSafeArray<T>& container)
    {
        return CComSafeArrayIterator<T>(container, 0);
    }
    template <typename T>
    CComSafeArrayIterator<T> end(CComSafeArray<T>& container)
    {
        return CComSafeArrayIterator<T>(container, container.getSize());
    }
    

    Notice that begin() is zero position, end() is getSize() position.

    And writing an iterator is not rocket science. Just a few functions.
    The most important is to know what you need to iterate. In your case: the container reference(pointer) and the current position. Iterating is just moving the position. Accessing is via container and position. Comparing is via comparing position.

    template <typename T>
    class CComSafeArrayIterator {
    public:
       CComSafeArrayIterator(CComSafeArray<T>& container, ULONG position) 
       : container(&container), 
       position(position) 
       {}
    
       // prefix ++it
       CComSafeArrayIterator& operator ++() { ++position; return *this; }
       // postfix it++
       CComSafeArrayIterator operator ++(int) { 
           CComSafeArrayIterator prev = *this; 
           ++position; 
           return prev; 
       }
       // access by ->: it-> !! ony if getAt return reference not value 
       const T* operator -> () const {
          return &(container->getAt(position));
       }
       // access by *: *it
       const T& operator * () const {
          return container->getAt(position);
       }
       // comparing
       friend bool operator == (const CComSafeArrayIterator<T>& l, 
                                const CComSafeArrayIterator<T>& r)
       {
          return l.position == r.position;
       }
       friend bool operator != (const CComSafeArrayIterator<T>& l, 
                                const CComSafeArrayIterator<T>& r)
       {
          return l.position != r.position;
       }
    
    
    
    private:
       // this is what you need
       CComSafeArray<T>* container;
       ULONG position;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some functions that use opengl to draw lines on the screen (health
I'm developing a XPCOM C++ component that have to use some RegExps functions Does
I have some code in a couple of different functions that looks something like
I have created some functional categories that I use to show/hide markup elements. However,
I have a Base Router where i define some functions that need to be
I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I have some setup code in my functions.php file that sets permalinks and adds
I have a c# project that calls some functions from a c dll, the
I have to create a wrapper DLL that exports some symbols (functions). Within its
I have a Python script that is using some closed-box Python functions (i.e. I

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.