25

What would be the most generic syntax for the following function :

template<IteratorType> void myFunction(const IteratorType& myIterator)
{
    _ptr = &myIterator[0];
}

It take an iterator myIterator (it can be a raw pointer) and the goal is to assign the address of the object pointed by myIterator to a raw pointer _ptr. Currently I use &myIterator[0] but I realized that only random access iterators have the operator [].

So is there a syntax that will work with all type of standard iterators and pointers ?

2
  • 2
    This might be a bit trickier than what you are showing in the case there the stored element overrides operator&... Commented Aug 29, 2012 at 13:55
  • @DavidRodríguez-dribeas: Indeed (although these days we've got std::addressof rather than that gibberish) Commented Aug 29, 2012 at 14:26

4 Answers 4

40

You can dereference pointer and then take address of object.

template<IteratorType> void myFunction(const IteratorType& myIterator)
{
    _ptr = &(*myIterator);
}
3
  • 3
    Note that this will fail if the stored object overloads operator& (yes, you should not overload operator& yet it is allowed in the language) Commented Aug 29, 2012 at 13:56
  • 11
    No need for parentheses in &*myIterator. Commented Aug 29, 2012 at 14:54
  • 2
    C++20: std::to_address. Commented Dec 8, 2023 at 13:33
30

According to standard * operator return a reference so in my opinion the best way is &*myIterator, but since the class may overloaded the & operator the best way is std::addressof(*myIterator) that work for all classes

3
  • 9
    +1 for std::addressof. If there is no C++11, one can use boost::addressof.
    – ForEveR
    Commented Aug 29, 2012 at 13:49
  • Only forward iterators promise to return references, input and output iterators can return proxy objects, which & is inappropriate for.
    – Caleth
    Commented Jun 24, 2021 at 10:27
  • You must guarantee that whatever iterator you apply that to, must be valid. I.e. a non-end iterator. Otherwise... you know... demons.
    – Adrian
    Commented Oct 30, 2021 at 3:18
8

All iterators are required to have operator * (24.2.2:2), so you can write

_ptr = &*myIterator;

However, this is not valid for output iterators, where *r is only valid on the left hand side of an assignment operation (24.2.4:2).

Also note that it is not necessarily true that *r will provide a value that & can sensibly be applied to; the specialization std::vector<bool> (23.3.7) has a reference type that is not bool &, for example. In your case the assignment _ptr = &*myIterator would catch this, assuming that _ptr is an appropriate pointer type, and you would get a compile failure.

1
  • You must guarantee that whatever iterator you apply that to, must be valid. I.e. a non-end iterator. Otherwise... you know... demons.
    – Adrian
    Commented Oct 30, 2021 at 3:19
1

I hope you guys are all alive right now. If your iterator is an iterator for std::vector, there is a nicer way to get the address that the iterator is holding. You can use:

auto *ptr = myIterator._Ptr;

I hope I could give a proper answer to your question.

1
  • 1
    This unfortunately is not a portable solution. Any identifier starting with '_' followed by a capital letter is reserved as an implementation detail and isn't guaranteed to be consistent in future versions of even the same implementation. In short, don't use it.
    – owacoder
    Commented Aug 27, 2021 at 18:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.