How to specialize std::begin?
Asked Answered
M

3

12

I'm trying to specialize std::begin for a custom container. I'm doing this because I want to use range-based for with the container. This is what I have:

class stackiterator { … };
class stack { … };

#include <iterator>

template <> stackiterator std::begin(stack& S)
{
  return S.GetBottom();
}

I get the following error at the definition of my begin specialization:

No function template matches function template specialization 'begin'

What am I doing wrong?

Motorbus answered 16/7, 2015 at 5:15 Comment(11)
Create a begin member function for your stack class that returns an iterator and you wont need template specialization.Godforsaken
Why don't you implement stack::begin() and just use the implementation of std::begin() as is?Rockaway
@RSahu Because the naming conventions of the project use PascalCase for functions.Motorbus
Or write a free begin that is found by ADL. You shouldn't really be writing std::begin(stuff) anyway.Uproar
@AkashPradhan I'd prefer to specialize std::begin because [see my previous comment].Motorbus
@Uproar That too would be against the project's naming conventions. Why not std::begin(stuff)?Motorbus
Because it's not how customization points are supposed to be used, and will not work with range-based for, for instance; and because it's impossible; specializing a function template can't change the return type, and the one you want to specialize returns "whatever member begin() returns", which obviously won't work if you don't have a member begin().Uproar
@zenith, So that you can find other begin functions through ADL. It's messy and Eric Niebler proposed a solution to make std::begin (and other customization points) do all that so that we don't end up having a repetitive bunch of using statements at the top of functions that grows larger as the list of customization points grows.Proliferate
I would think that the standard takes priority over coding conventions... You should double-check about writing a member begin function.Mothy
@Uproar According to this answer, you can specialize std::begin() to make it work with range-based for. Oh and I didn't know that specializations can't change return types, thanks.Motorbus
That's a defect changed by CWG 1442.Uproar
U
17

I'm trying to specialize std::begin for a custom container. I'm doing this because I want to use range-based for with the container.

You are barking up the wrong tree. Range-based for does not use std::begin at all. For class types, the compiler looks up member begin and end directly, and if neither is found, does an ADL-only lookup for free begin and end in the associated namespaces. Ordinary unqualified lookup is not performed; there's no way for std::begin to be picked up if your class is not in the std namespace.

Even if the specialization you want to do is possible (it isn't unless you introduce a member begin() - an explicit specialization for a function template can't change the return type, and the overload at issue returns "whatever member begin() returns"; and if you do introduce a member begin(), why are you specializing std::begin to do what it would have done anyway?), you still won't be able to use it with a range-based for.

Uproar answered 16/7, 2015 at 5:41 Comment(2)
Then what does this (accepted) answer mean when it says that range-based for compatibility can be achieved by specializing std::begin? Is it just wrong?Motorbus
@zenith It's outdated.Uproar
R
2

Leaving aside the policy and semantic issues of whether you should specialize a function template from the std namespace,

The following snippet does not work:

class stackiterator {};
struct stack { stackiterator Begin() { return stackiterator{};} };

#include <iterator>

namespace std
{
   template <> stackiterator begin<stack>(stack& S)
   {
      return S.Begin();
   }
}

However, the following snippet works just fine:

class stackiterator {};
struct stack { stackiterator begin() { return stackiterator{};} };

#include <iterator>

namespace std
{
   template <> stackiterator begin<stack>(stack& S)
   {
      return S.begin();
   }
}

The key difference is the presence of Begin() vs begin() as a member function of stack. std::begin() is defined as:

template <class C> auto begin(C& c) -> decltype(c.begin());
template <class C> auto begin(const C& c) -> decltype(c.begin());

When you specialize a function template, you must still keep the return type the same. When you don't have begin() as a member of Stack, the compiler does not know how to determine the return type.

That is the reason for error produced by the compiler.

BTW, there is another SO post that partially answers what can be specialized and what can't be specialized.

Looking at the part of the standard that deals with std::begin(), Section 24.3, I don't see anything about not being able to specialize std::begin().

Rockaway answered 17/7, 2015 at 18:24 Comment(2)
If you already have a member begin(), why on earth would you want to specialize std::begin()?Uproar
@TC, was that comment for me or the OP?Rockaway
B
1

The right way to add a free function begin that enables for(:) loops is to add, in the namespace of stack, a begin(stack&) and begin(stack const&) function that returns a iterator and const_iterator respectively (and ditto for end)

The other way is to add a member begin() and end() to stack.

Specializing std::begin is bad practice for a number of reasons, not the least of which is that not all for(:) loops will work with it (the lookup rules where changed in the resolution of this defect report). Overloading std::begin is undefined behavior (you may not overload functions in namespace std under the standard: doing so makes your program ill-formed).

This is how it has to be done, even if it violates the naming convention of your project.

Bookmark answered 16/7, 2015 at 14:28 Comment(1)
Note that it was supposed to be a specialization of std::begin (not an overload) since those are allowed by the standard.Motorbus

© 2022 - 2024 — McMap. All rights reserved.