2

Recently I have been working on a code base which uses MFC and objects such as CArray<T, U>.

Some parts of new code which has been written make use of the STL and <algorithm> library.

For example

CArray<int int> carray;
carray // do stuff
std::vector<int> stlvector(begin(carray), end(carray));
stlvector.dostuff() // do stuff

I recently asked a question about creating iterators for a class such as CArray, which I do not have access to.

I now have some further questions about this. Here is my first question:

  • Should the begin and end functions be inside the std namespace?

I will ask the other questions separately and provide links to them shortly, replacing this line of text when those links are available.

4
  • 2
    begin and end need template specializations, en.cppreference.com/w/cpp/iterator/begin Commented Dec 13, 2021 at 10:52
  • 2
    "...Should the begin and end functions be inside the std namespace?..." no add them in your own namespace and rely on ADL to find them. See "...It is undefined behavior to add declarations or definitions to namespace std or to any namespace nested within std, with a few exceptions noted below..." en.cppreference.com/w/cpp/language/extending_std Commented Dec 13, 2021 at 10:53
  • I'd replace CArray<int int> with std::vector<int> alltogether. Commented Dec 13, 2021 at 11:22
  • @Jabberwocky Can't be done Commented Dec 13, 2021 at 11:31

2 Answers 2

1

It can be useful to look at a non-trivial use of begin:

CArray<int> carray;
for (auto c : carray){
}

The begin is hidden in the range-for loop! Which begin(carray) is this? The name lookup rules here say that only argument-dependent lookup is done. There's no attempt to look at std::begin, since std is not at all related to CArray.

This makes sense. CArrayIterator begin(CArray) is a function that belongs to CArray, and therefore should be in the same namespace.

Sign up to request clarification or add additional context in comments.

Comments

0

To properly define the function, it should be defined in the same namespace as CArray. To properly use it generically, it should be used like, similar to the two-step swap idiom:

{
    using std::begin;
    begin(my_carray);
}

Alternative you might consider using some C++20 features that can wrap CArray, such as std::span and std::ranges::view_interface

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.